Python testing frameworks
In python, there is a testing framework that comes with the standard library: unittest. It's quite simple to use and revolves around that simplicity.
Another popular framework used in python programming is pytest. With easy to use testing methods and functions, pytest allows quick test write ups and many features that make it useful.
Unittest
First comes unittest.
This is a basic feature of python so you can simple type import unittest
to use it. The actual testing is done with classes, setting up and tearing down according to tests.
For example, the following code makes a class with unittest.
import unittest
class TestingAdditions(unittest.TestCase):
def test_one_plus_one_is_two(self):
self.assertEqual(1 + 1, 2)
Grouping tests into a class is quite simple and easy to understand as functions and tests are tied together according to relatibiltiy. However, as tests become complicated, unittest may not be the ideal testing framework to use.
First of all, making classes is quite an nuisance. At first it's a great idea. But with more and more tests, code repetition and unnecessary class constructions may not be optimal choices. Basically, even if you have a single unit test, a class is used.
Second, the outcome of the tests are limited to 3 choices: "OK", "FAILED", "ERROR". This means that it's simple to analyze and view test results but it also means that in some circumstances, outcomes are over simplified.
Pytest
Then comes pytest. Pytest is, in my opinion, a testing framework based on pythonic ideologies such as simplicity. It uses functions and basic assert
to orchestrate tests.
You can group tests into classes, but usually better with files. The key feature of pytest is fixture. A fixture is basically code that is repeated elsewhere but only written once. So a single fixture can be called upon by tests many times. This is useful when using mock objects or data and different tests are using the same object.
Another key feature is the 'conftest.py' file. This file contains and allows tests to run smoothly and can be used to 'setup' and 'teardown'. Also, pytest works better when made into modules. Meaning 'init.py' files. This is because one can use multiple conftest files for a single test. But the weight of using multiple files can be varied depending on the test by appropriately adjusting testing layers.
And finally, pytest can integrate unittest into its tests. Meaning one can write tests with unittest and run them with pytest.
Conclusion
From my perspective, having used both unittest and pytest, and also having made a pytest plugin, I find pytest better to use.
It's not only easy to use, but simple, quicker than unittest. Even if one has to use unittest for some reason, pytest actually can execute unittest tests so no problem there either.
'Programming Languages > Python' 카테고리의 다른 글
Python Pytest 관해서 (0) | 2022.06.01 |
---|---|
파이썬 가상환경을 왜 써야할까...? (0) | 2022.05.31 |