I’ve an strong believe that programming in different languages is something that all developers should do, and do it so you face different problems and apply different technics to solve them.
Python is great for a lot of tasks, and I’m using it for tooling a lot, but I’m not doing much Unit Testing on it so I decide to improve and expand my tests.
So here is what I learn so far, if you want to skip these and go directly to the code here is the git repo with every example bellow.
I’ve decide to use nose as test runner, and PyHamcrest as the assertion framework, specially since I familiar with the popular Java equivalent.
So lest start with the first example, testing this small class saved in a file named: calc.py
And here our first test in a file named: test_calc.py
To run the test just this in the same directory that both files are place:
The first test is really simple and just we call our class methods and check in hamcrest style that result is what we expect.
Let’s make our class a bit more interesting adding a method that give the last result
With the new test we could add some more bits, like concatenating expressions
Now to go really crazy lets make our class to been able to repeat operation using some lambdas:
Now for test we could test that this new method work, even creating a new lambda for it:
Now for modification of our class lets create some validation of parameters raising exceptions
and in our test will add checking that the exceptions are raise when they should:
I think this if enough for today, next day maybe a different language.