This is the example project for Python Testing 101: doctest, part of the Python Testing 101 series from Automation Panda. It will work for Python 2 and 3.
This project has two packages:
com.automationpanda.examplecontainscalc.py, which has a basic Calculator class.com.automationpanda.testscontainstest_calc.py, which containsunittesttest cases.
To run tests from the example project root directory, run one of the following commands:
python -m unittest discoverto discover all unit tests in a projectpython -m unittest com.automationpanda.tests.test_calcto run the test module by namepython -m unittest com.automationpanda.tests.test_calc.CalculatorTestto run a TestCase class by namepython -m unittest com/automationpanda/tests/test_calc.pyto run the test module by file path
Note that unittest tests must be in a Python package from the project root,
meaning that the tests directory needs __init__.py.
To generate XML test reports, install unittest-xml-reporting and run:
python -m com.automationpanda.tests.test_calc
XML reports will be printed to the test-reports directory.