nose collects tests from unittest.TestCase subclasses. But we can also write simple test functions, as well as test classes that are not subclasses of unittest.TestCase. nose also supplies a number of helpful functions for writing timed tests, testing for exceptions, and other common use cases.
nose collects tests automatically, as long as you follow some simple guidelines for organizing your library and test code. There's no need to manually collect test cases into test suites. Running tests is responsive, since nose begins running tests as soon as the first test module is loaded.
Look at the below example, in which we have created a square method and run a unit test using the test() function.
Save the below code in the file named 'test.py'.
def square(x):
return x**2
def test():
assert square(3) == 8
As we can see the assert statement is logically wrong as the value of 32 should be 9. We will now run the test using the below command.
nosetests test.py
The output of the above is
nosetest.test ... FAIL
================================================================
FAIL: nosetest.test
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:Python34libsite-packages\nosecase.py", line 198, in runTest
self.test(*self.arg)
File "C:Python34\nosetest.py", line 6, in test_answer
assert square(3) == 8
AssertionError
----------------------------------------------------------------------
Ran 1 test in 0.000s
FAILED (failures = 1)
The test fails and the output shows the summary specifying why it failed. The platform used to run the tests and other info.
We can also run tests without using nosetests scripts like we can use nose in a test script using
import nose
nose.main()
If we don't want the test script to exit with 0 on success and 1 on failure we can use the below
import nose
nose.run()
Comments