pytest
is a Python testing framework to check that your code is doing what they should!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| # Import pytest
import pytest
# We define tests as functions
def test_something():
pass
# We can also group our test methods into classes
class TestUS1():
def test_normal_use(self):
pass
def test_no_items(self):
pass
def test_one_item(self):
pass
|
- Any python files that start with
test_
or end with _test
are included by pytest - Any class that starts with
Test
are included by pytest - Any method that starts with
test_
are executed by pytest
Testing for Exceptions
1
2
| with pytest.raises(ExceptionName):
doSomethingThatRaisesAnException
|
Fixtures
If you have code which produces an object that runs for several tests, you can group them into a fixture
1
2
3
4
5
6
7
8
9
10
| @pytest.fixture
def mySystem():
# do stuff
sys = SomeSystemThing()
sys.setValue(7)
return sys
def test_checking(mySystem):
# Fixture name here ^^^^
assert mySystem.check() == True
|
Setup and Teardown
Another way to rerun the same piece of code multiple times is through a setup and teardown procedure.
Consider this excerpt from StackOverflow
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| lh = <got log handler from logger module>
class TestClass:
@classmethod
def setup_class(cls):
lh.info("starting class: {} execution".format(cls.__name__))
@classmethod
def teardown_class(cls):
lh.info("starting class: {} execution".format(cls.__name__))
def setup_method(self, method):
lh.info("starting execution of tc: {}".format(method.__name__))
def teardown_method(self, method):
lh.info("starting execution of tc: {}".format(method.__name__))
def test_tc1(self):
<tc_content>
assert
def test_tc2(self):
<tc_content>
assert
|
Now when I run my tests, when the TestClass execution is starting, it logs the details for when it is beginning execution, when it is ending execution and same for the methods..