Unit Test with Pytest
Unit testing involves testing individual components of software in isolation to ensure they function correctly. Automated frameworks facilitate this process, which is integral to ensuring that new changes do not disrupt existing functionality. Unit tests also serve as practical documentation and encourage better software design. This testing method boosts development speed and confidence by confirming component reliability before integration. Early bug detection through unit testing also helps minimize future repair costs and efforts.
pytest
Pytest is one of the best tools that you can use to boost your testing productivity for Python codes.
Install
pip install pytest
pip install pytest-cov
pytest --cov
: this returns a coverage of test functionscoverage html
: log test results in html format
Example
pytest
is a libarary for testing. You can run your unit test code by
|
|
If you wanna create a directory with several testing files, then just create __init__.py
and put it inside the test dir.
- Then, just run
pytest test_dir
|
|
- If you want to intentially raise an error, then you can do it by
pytest.raises("SomeErrorType")
Note that you can write a warning message like
|
|