My testing script looks as follows:
import os
import sys
from unittest import defaultTestLoader as loader, TextTestRunner
path_to_my_project = os.path.dirname(os.path.abspath(__file__)) + '/../'
sys.path.insert(0, path_to_my_project)
suite = loader.discover('my_project')
runner = TextTestRunner()
runner.run(suite)
If I run this script, the output is:
$ python3 runtest.py
.....F.....
======================================================================
FAIL: test_insert (fate.test.test_operators.OperatorTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/chiel/Projects/tfate/libs/fate/../fate/test/test_operators.py", line 16, in test_insert
self.assertEqual(expected, self.session.text[:14])
AssertionError: 'Foo import sys$' != 'Foo import sys'
- Foo import sys$
? -
+ Foo import sys
----------------------------------------------------------------------
Ran 12 tests in 0.030s
FAILED (failures=1)
And exit code zero:
$ echo $?
0
However, the Python documentation states that "By default main calls sys.exit()
with an exit code indicating success or failure of the tests run."
What is wrong with my script?
An exception object is created when a Python script raises an exception. If the script explicitly doesn't handle the exception, the program will be forced to terminate abruptly.
All contributed code should be well tested. This should be done through both doctests and standard unit tests using pytest. All public functions, classes, methods, etc. must have a docstring that follows the numpydoc conventions.
If your function is supposed to assert something and raise an error, give it wrong information and check if it does raise the right error. If your function takes an object and modifies it, test if the new state of your object is as expected.
Running unit tests in parallel is a new feature in xUnit.net version 2. There are two essential motivations that drove us to not only enable parallelization, but also for it to be a feature that's enabled by default: As unit testing has become more prevalent, so too have the number of unit tests.
The code is not using unittest.main
. You need to check the result using TestResult.wasSuccessful
and call sys.exit
manually.
import sys
....
ret = not runner.run(suite).wasSuccessful()
sys.exit(ret)
I had some trouble getting TextTestRunner
results. For those like me, here is how it works:
"""Run all tests inside of *_test.py modules located in the same directory."""
import sys
import unittest
if __name__ == '__main__':
test_suite = unittest.defaultTestLoader.discover('.', '*_test.py')
test_runner = unittest.TextTestRunner(resultclass=unittest.TextTestResult)
result = test_runner.run(test_suite)
sys.exit(not result.wasSuccessful())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With