Python testing

Jimmy (xiaoke) Shen
1 min readAug 17, 2022

--

Which testing tools that we can use for python?

As a software engineer, it is a common practice to add test cases for your software and program. General, we need Unit Tests and Integration Tests. [5] gave a pretty clear explanation.

We have 3 ways to run python tesing.

  • unittest
  • nose or nose2
  • pytest

Originally, I was using pytest, it is pretty convenient. However, I got quite a lot of Moudular import error, especially in the docker environment. So I switched to unittest and there is no issue. If you wanna to know how to use the “unittest”, please check [5].

Useful commands for unittest

  • Test all unittest files under a specified folder [6]
python -m unittest discover <test_directory>
# or
python -m unittest discover -s <directory> -p '*_test.py'

To be updated for other commands that I feel helpful.

Reference

[1]https://www.guru99.com/pytest-tutorial.html

[2]https://docs.pytest.org/en/7.1.x/

[3]https://stackoverflow.com/questions/41748464/pytest-cannot-import-module-while-python-can

[4] https://medium.com/@dirk.avery/pytest-modulenotfounderror-no-module-named-requests-a770e6926ac5

[5] https://realpython.com/python-testing/

[6]https://stackoverflow.com/questions/1732438/how-do-i-run-all-python-unit-tests-in-a-directory

--

--

No responses yet