We should also test whether they fail when given bad input. Passing the test with good input is not enough. An error is any other sort of exception raised in the code you're testing or the unit test case itself.A failure is a call to assertEqual or assertRaises, that fails because the asserted condition is not true or the expected exception was not raised.When a test case doesn't pass, unittest distinguishes between failures and errors: That's because there was no explicit return statement, the function returned None, the Python null value. In this case, the call to assertEqual() raised an AssertionError because it was expecting toHex(1) to return '1', but it didn't. Note that each test class must inherit from unittest.TestCase.įor each failed test case, unittest displays the trace information showing exactly what happened. Then, it runs each test case which is a method within a class in hexatest.py. When we run the hexatest.py script, it acually runs unittest.main(). If we test run with hexatest.py, we get the following output:įile "hexatest.py", line 15, in testToHexa We should write a test that fails, then code until it passes. If the tests succeed before we've written any code, our tests aren't testing our code at all. Once we have a test case, we can start coding the toHexa() function.įirst, we should stub it out as an empty function and make sure the tests fail. If the two values are equal, assertEqual() will do nothing. If they don't match, assertEqual() will raise an exception and the test will fail. The TestCase class provides a method, assertEqual(), to check whether the return value from toHexa() and haxa value of pair match.The toHexa() function takes an integer and return a string for hexa. Here we call the toHexa() function which hasn't been written yet, but once it is, this is the line that will call it.The pair is a tuple of decimal/hexa pairs to test every possible input, but we should try to test all the obvious edge cases.The TestCase class provides many useful methods which we can use in our test case to test specific conditions. We subclassed the TestCase class of the unittest module:.Import hexa # This is the code being tested The toHexa() function must return the hexa number for all integers from 1 to 65536.
0 Comments
Leave a Reply. |