Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/mgobeaalcoba/unit_testing_python

Repository with data and practice about unit testing in Python
https://github.com/mgobeaalcoba/unit_testing_python

coverage doctest mock pytest unit-testing unittest

Last synced: about 14 hours ago
JSON representation

Repository with data and practice about unit testing in Python

Awesome Lists containing this project

README

        

# Testing in Python

## Types of Testing:

### Definitions

- **Unit Testing:** Verifies the correct operation of individual code units, such as functions or methods.
- **Integration Testing:** Ensures that different modules or components work well together.
- **System Testing:** Evaluates the complete system to ensure that it meets the specified requirements.
- **Acceptance Testing:** Verifies that the software meets the needs of the end user.
- **Regression Testing:** Ensures that changes or updates have not introduced new errors in existing functionalities.

### Additional Information

- **Unit Testing:** It is usually automated and is the basis of TDD (Test Driven Development).
- **Integration Testing:** I added the detail about testing the interaction of modules already tested individually and the ways in which it can be done (incremental vs. "big bang").
- **System Testing:** I incorporated a reference to non-functional requirements (performance, security, etc.) that are a key part of this testing phase.
- **Acceptance Testing:** It is usually performed by the client or the QA team on their behalf. It is often divided into two subtypes: user acceptance testing (UAT) and operational acceptance testing.
- **Regression Testing:** I added a comment about automation, which is very relevant for this type of testing, especially in large projects.
- **Smoke Testing:** It is a preliminary test to reveal simple failures severe enough to reject a prospective software release.
- **Sanity Testing:** It is a narrow regression test that focuses on one or a few areas of functionality after a change.
- **Exploratory Testing:** It is a simultaneous learning, test design, and test execution process.

## Tools for Testing in Python

- **unittest:** The built-in Python library for unit testing.
- **pytest:** A third-party library that provides a more advanced testing framework.
- **doctest:** A module that allows you to test your code by running examples embedded in the documentation and verifying that they produce the expected results.
- **mock:** A library for testing in Python that allows you to replace parts of your system under test with mock objects and make assertions about how they have been used.
- **coverage:** A tool that measures code coverage of Python programs.

## Best Practices for Testing in Python

- **Write Testable Code:** Make sure your code is modular, loosely coupled, and follows best practices.
- **Use Descriptive Names:** Choose meaningful names for your test methods and variables.
- **Keep Tests Simple and Readable:** Avoid complex logic in your tests and make them easy to understand.
- **Run Tests Automatically:** Use continuous integration tools to run your tests automatically on every code change.
- **Use Mocks and Stubs Sparingly:** Only use mocks and stubs when necessary, as they can make tests harder to understand and maintain.
- **Test Edge Cases:** Make sure to test boundary conditions and edge cases to ensure your code works in all scenarios.
- **Refactor Tests:** Refactor your tests regularly to keep them up to date with changes in your codebase.
- **Use Code Coverage Tools:** Measure code coverage to ensure that your tests are thorough and cover all parts of your code.
- **Write Documentation:** Document your tests to explain what they are testing and why.
- **Collaborate:** Involve your team in testing and code reviews to catch bugs early and improve code quality.

## Conclusion

Testing is an essential part of the software development process, and Python provides a variety of tools and libraries to help you write effective tests. By following best practices and using the right tools, you can ensure that your code is reliable, maintainable, and bug-free. Remember that testing is an ongoing process, and it is important to continuously test and improve your code to deliver high-quality software.

## References

- [Python Testing Tools Taxonomy](https://wiki.python.org/moin/PythonTestingToolsTaxonomy)
- [Python Testing with pytest](https://docs.pytest.org/en/latest/)
- [Python unittest Library](https://docs.python.org/3/library/unittest.html)
- [Python doctest Module](https://docs.python.org/3/library/doctest.html)
- [Python mock Library](https://docs.python.org/3/library/unittest.mock.html)
- [Coverage.py Documentation](https://coverage.readthedocs.io/en/coverage-5.5/)
- [The Art of Unit Testing](https://www.amazon.com/Art-Unit-Testing-examples/dp/1617290890) by Roy Osherove

## Further Reading

- [Test-Driven Development (TDD): An Overview](https://www.freecodecamp.org/news/an-introduction-to-test-driven-development-tdd-61a3e8bd88c8/)
- [The Practical Test Pyramid](https://martinfowler.com/articles/practical-test-pyramid.html)
- [The Testing Trophy: A Test Strategy](https://kentcdodds.com/blog/testing-implementation-details)
- [The Way of Testivus](https://www.artima.com/weblogs/viewpost.jsp?thread=203994)
- [The Art of Software Testing](https://www.amazon.com/Art-Software-Testing-Glenford-Myers/dp/0471469122) by Glenford J. Myers

## About the Author

This article was written by [Maraino Gobea Alcoba](https://www.linkedin.com/in/mariano-gobea-alcoba/), a software and data engineer with a passion for testing and quality assurance. Mariano has experience working on a variety of projects, from web applications to machine learning models, and is always looking for ways to improve code quality and reliability. You can connect with Mariano on [LinkedIn](https://www.linkedin.com/in/mariano-gobea-alcoba/) or [GitHub](https://github.com/Mgobeaalcoba) to learn more about his work.

## LICENSE (MIT)

Copyright (c) 2021 Mariano Gobea Alcoba