Archives For

py.test from behind a firewall

Brian —  April 9, 2012 — 2 Comments

I wanted to try out pytest (or py.test, I’ve heard it both ways).
But I’m behind a firewall, so pip and easy_install don’t work that great.
I’m also running cygwin, with several versions of python so I can test compatibility of scripts across 2.6, 2.7, and 3.2.

Trial and error gets me to these steps that seem to work.

  1. download py
  2. unpack it to directory py147 (whatever you want to name it)
  3. download pytest
  4.  unpack it to directory pytest223 (again, whatever you want to name it)
  5. install py (see below)
  6. install pytest (see below)

In this example, I’ve got a symbolic link in my path called python27 that links to my python 2.7 executable.

[bash]
> cd py147
> python27 setup.py install
> cd ../pytest223
> python27 setup.py install

# now test it by running one of the pytest included scripts
> cd testing
> python27 -m pytest test_python.py
[/bash]

Splendid.
Now getting on to the task of examining it to see if it’s functionality meets my needs.

Update 4-June-2012:

I just tried this in a new machine and ran into an issue that distribute wasn’t installed.

This is fixed simply by downloading and installing  it and trying the rest again.

  1. download distribute
  2. unpack it to directory dist (whatever you want to name it)
  3. cd dist
  4. python27 setup.py install
  5. go back up to installing py and py.test (see above)

Testing in Python

Brian —  April 9, 2012 — Leave a comment

I’ve been using Python for writing test code for many years now.
I’ve used several test frameworks, most of them proprietary.
However, I’d like to utilize some of the open source frameworks available.

Right now, I’m doing two things.
1. Writing my own framework.
2. Investigating pytest.

Hopefully I will have some results from both experiences to share.

Using an output string stream is a handy way to convert a number to a string.

However, what if you want to zero pad the number.

Well, that’s where iomanip comes in handy.

[code language="cpp"]
#include <iomanip> // for setw, setfill
#include <iostream> // for cout
#include <sstream> // for ostringstream
#include <string> // for string

using namespace std;

int main ()
{
int i = 1;

ostringstream ss;
ss << setw( 3 ) << setfill( '0' ) << i;
string s = ss.str();

cout << "i: " << i << endl;
cout << "s: " << s << endl;
return 0;
}

[/code]

output:
[bash]
i: 1
s: 001
[/bash]

C++ allows you to re-use variables in nested scopes.

A confusion arises sometimes, especially with those new to C++ rules, as to the variables at the top of a for loop.

In a for loop, such as for (int i = 0; i < 2; i++) { ... }, does the int i belong to the outer scope or the scope within the for loop.

The answer is that it belongs to the inner loop, and hides any outer variables, if they exist.
Of course, this is best shown with an example.

Three variables: i, j, k
Variable i is defined both in the outer scope and as a loop index variable defined in the top of the for statement.
Variable j is defined both in the outer scope and within the curly brackets of the for loop.
Variable k is only defined in the outer scope.

The treatment of i is the same as j. It’s as if all of the statements at the top of the for loop are inside the curly brackets of the for loop.

Sample Code:
[code lang="cpp"]
#include
using namespace std;

void report(string label, int i, int j, int k)
{
cout << label << " i:" << i << " j: " << j << " k: " << k << endl;
}

int main ()
{
int i = 123;
int j = 456;
int k = 789;

report("before loop", i, j, k);
cout << endl;

// inner scope i hides outer scope i
for (int i = 0; i < 2; i++)
{
// inner scope j hides outer scope j
// outer scope k not hidden
int j = i;
report("in for loop", i, j, k);
}
cout << endl;
report("after loop ", i, j, k);
return 0;
}
[/code]

output:

[bash] > g++ -Wall -o scope for_loop_scope.cpp
> ./scope
before loop i:123 j: 456 k: 789

in for loop i:0 j: 0 k: 789
in for loop i:1 j: 1 k: 789

after loop i:123 j: 456 k: 789
[/bash]

Please leave a comment if you feel I’ve gotten something wrong here.
If your compiler does something different, I want to hear about it.

Quick instructions for compiling small C++ projects on the command line in bash.
Continue Reading…