- What are you testing?
- What should it do?
- What is the actual output?
- What is the expected output?
- Should test a single aspect of a component
- Usually this means test should contain only a single assert
- Tests should not rely on running in any particular order
- Should be able to run repeatedly with same results given same input
- Should be fast
- Should be run often
- Many tools allow tests to run on file save, or even after every keystroke!
- Prefer simple assert statements
- Frameworks often contain advanced, and sometimes very handy, assertions.
- E.g.
assertRendersTemplate('home.html')
- These are awesome, but use sparingly. Simple asserts are usually more readable.
- E.g.
- Frameworks often contain advanced, and sometimes very handy, assertions.
def sum(a,b):
""" sum two numeric arguments """
if isinstance(a, (int, float)) and isinstance(b, (int, float)):
return a + b
raise TypeError('argument must be int or float')
def test_should_sum_two_integers():
expected = 3
actual = sum(1,2)
assert expected is actual, "sum of 1 and 2 should be 3"
def test_should_sum_two_larger_integers():
expected = 1333332
actual = sum(1234567,98765)
assert expected is actual, "sum of 1234567 and 98765 should be 1333332"
def test_should_sum_negative_numbers():
expected = 3
actual = sum(5,-2)
assert expected is actual, "sum of 5 and -2 should be 3"
def test_should_handle_single_argument():
expected = 2
actual = sum(2)
assert expected is actual, "sum of 2 and nothing should be 2 because 2nd argument defaults to 0"
Note: Different languages handle floating point precision differently. Adjust your assertion as needed.
def test_should_sum_floats():
expected = 5.0
actual = sum(3.5,1.5)
assert expected == actual, "sum of 3.5 and 1.5 should be 5.0"
def test_should_sum_integer_and_float():
expected = 5.5
actual = sum(3.5,2)
assert expected == actual, "sum of 3.5 and 2 should be 5.5"
def test_should_accept_only_numbers():
# consult docs for how your test framework handles testing exceptions
with pytest.raises(TypeError):
sum('1',3)