Hopefully to encourage people to join in, I added unit-tests to the PY_RPG game logic. What does this mean? I'll get to that in another post, for now, lets cover "what the heck is TDD"?
What is TDD ( using nose ) ?
To help follow along with code, I made a repo here - https://github.com/devdave/py_rpg1_intro_to_nose
TDD stands for Test driven development, it's a methodology for writing software that a programmer expects to get complicated or have someone else work on together ( which is almost everything a programmer will write ).
Commit 0 - Create our new project!
Most software programs can be broken down to three parts. Input, process, and output. For a simple math function, you want to input "1+1", process it, get bacon, and get a integer 2 returned. Unit testing is a way of saying "Given a fixed input, I expect this output."
With TDD, the math function doesn't even exist yet!
Instead of writing a my_math.py file 1st, you would write a test_my_math.py file first!
in test_my_math.py you would write
import my_math
and then run nosetests at the command line.
Immediately nose tests will spit back ImportError: No module named my_math
Commit 1 - Add our unit test!
So go ahead and make a my_math.pyfile and run nosetests again. This time, nose will finish and print out
Ran 0 tests in 0.010s
OK
Kind of useless at this point. So let's write a unit test!
In test_my_math.py
import my_math
from nose.tools import eq_
def test_my_math__math():
expected = 2
actual = my_math.math("1 + 1")
eq_(expected, actual)
Note: I imported eq_ which is short for equals and is really nice for seeing if two variables == each other. If they don't nose will try to tell you what they look like/are.
Now when you run nosetests it will say
AttributeError: 'module' object has no attribute 'math'
Commit 2 - Add a unit test! ~Note I did this commit out of order, my apologies!~
So time to write a math function!
def math(input):
pass
Now run nosetests again.
Dilligently, nose will return the error
AssertionError: 2 != None
At this point you have a goal or something called the conditions of satisfaction. Conditions of Satisfaction ( I would shorten it to "C O S" in conversation" ) is what needs to be done for a project/contract be done, in this case you need your unit test to pass. You could take the short way by writing
def math(input):
return 2
but if you were working with someone like me in an office environment, they would never find your body.
So instead, we'd writing something like...
def math(input):
left, operand, right = input.split(input)
return left + right
and run nosetests one more time to get
Commit 3 - Make math pass the unit test correctly!
Ran 1 test in 0.000s
OK
Why Nose?
I am perpetually a lazy person, I don't like writing the same thing twice and I don't want to waste a lot of time learning a unit-test framework. I want to get stuff done and I don't want to spend a lot of time doing it.
There are a LOT of unit testing frameworks, it would be unfair to say one is superior to the other ( maybe that's true but it sometimes comes down to personal involvement & opinions ). In my case, I chose nose.
At the heart of a nose unit test, all you need is a file that starts with test_ and nosetests will find it, do its best to run it, and tell you what happened.
At the heart of a nose unit test function, all you need is something like
def test_assert_the_universe_is_correct():
assert 1+1
and nose will tell you 1 test passed
That's pretty much it. As long as an assert is True, nose is happy.
What next?
Write a def test_math__can_do_multiplication with an input like 1 * 5 where the output == integer 5. Modify math until both unit tests ( the current one, and the new one) pass.