To give a slightly contrived example, let's say I want to test that a function returns two numbers, and that the first one is smaller than the second one:
def test_length():
result = my_function()
assert len(result) == 2
def test_order()
a, b = my_function()
assert a < b
Here, if test_length
fails, then test_order
will fail too. Is it a best practice to write test_length
, or to skip it?
EDIT: note that in this situation, both tests are mostly independent from each other, each one can be run in isolation, or they could be run in reverse order, this does not matter. So none of these former questions
- How should I test the functionality of a function that uses other functions in it?
- Do I need unit test if I already have integration test?
- How to structure tests where one test is another test's setup?
- How to manage success dependency between unit tests
is a duplicate of the one above.
A
callsB
and returns the same result, should you test bothA
andB
". This is more about the tests being overlapping rather than the function(s) under test. (Though it's confusing as they're currently named).lambda: type('', (), {'__len__': lambda self: 2})()
will pass the first, but not the second.