Throughout this section on testing, you've worked with Python's built-in unittest module. As you might have expected, with Python's large package ecosystem, this is not the only popular testing framework used in the community.
Python Pytest
Python Pytest is another popular Python testing framework, which isn't part of Python's standard library, is pytest:
How Does Pytest Work
Python pytest is a wrapper around unittest that can make it faster and more intuitive to write tests. For example, it allows you to write functions instead of needing to define a class with methods. It also allows the use of the assert keyword directly.
With pytest, you could therefore write your tests like this, as described in their documentation:
# content of test_sample.py
def inc(x):
return x + 1
def test_answer():
assert inc(3) == 5
After installing pytest with pip, you could then run the test just by typing pytest into your CLI in the repository that your test file is in:
python -m pytest
pytest will automatically pick up all files that are named with test_ at the beginning and execute the assertions in those files.
Note: While this simplicity might sound exciting, there are also good reasons that you've learned about a more rigid test file structure and test class setup using unittest before. Keep those lessons in mind and keep your tests clear, descriptive, and separate from your code also when you're working with pytest instead.
Feel free to explore the pytest documentation in more detail if you're interested in learning more about this testing framework.
**Additional Resources
pytestDocumentation:pytest: helps you write better programs- Brian Oken: Productive pytest with PyCharm
- Pythontesting: Python Testing Resources
Summary: Python Pytest Frameworks
- There are other testing frameworks that are used besides the popular
unittestframework - The Python
pytestframework is a wrapper aroundunittest pytestgives a bit more freedom in writing tests