In order to generate XML reports, install unittest-xml-reporting and add the following “main” logic to the bottom of the test case module. Test output should look like this: > python -m unittest discover > python -m unittest com/automationpanda/tests/test_calc.py # Run all tests in the given Python file (useful for path completion) > python -m unittest _calc.CalculatorTest To launch tests from the command line, change directory to the project root directory and run the unittest module directly from the python command: # Discover and run all tests in the project calc import CalculatorĬlass CalculatorTest( unittest. The module contains a Calculator class with basic math methods:įrom com. The project has the following structure: example-py-unittest Īn example project named example-py-unittest is located in my GitHub python-testing-101 repository. Remember, a Python package is simply a directory with a file named “ _init_.py“. Test modules must be named “test_*.py” and must be put into packages in order for discovery to work when launching tests. Product code modules and unittest test code modules should be placed into separate Python packages within the same project. > pip install unittest-xml-reporting Project Structure However, additional modules may be installed with pip if you need them: > pip install unittest2 Installationīasic unittest does not need any special installation because it comes with Python. However, use the unittest2 backport for versions earlier than Python 2.7. Unittest is supported in both Python 2 and 3. unittest can also generate XML reports (like JUnit) using unittest-xml-reporting. Tests can be grouped and loaded using the TestSuite class and load methods, which together can build custom test runners. Each method in a TestCase subclass whose name starts with “test” will be run as a test case. All test case classes must inherit from TestCase. unittest provides a base class named TestCase, which provides methods for assertions and setup/cleanup routines. Inspired by JUnit, it is included with the standard CPython distribution. Unittest is the standard Python unit testing framework.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |