I keep hearing people say about how tests should be simple, maintainable, straightforward, but what happens with code re-usability in unit testing?
Let's take for example:
def test_some_1():
...some code
def test_some_2():
...code repeated from test_some_1
Wouldn't it be best to encapsulate the repeated code from the two tests in a function which holds the necessary assertions?
I have argued with some programmers about this and they disagree, they said that tests should be dumb, that code re-usability is not good here. The reason for this is because it is not very clear in the django console where the assertion actually failed, because the assertion was in the function, although I disagree because using it with nose would give you the name of the test and the traceback, although the guys disagreed again, stating that a test could be invoked individually without nose (and therefore you couldn't see all those details).
What do you guys think?
- Is it good to use code re-usability in unit tests?
- If re-usability can/must be used then how to overcome the other problem regarding pin-pointing the assertions?
setUp
andtearDown
? docs.python.org/2/library/unittest.html#unittest.TestCase.setUp – Septuagint