
Another popular Python testing framework, pytest is known for its simplicity and powerful features. It supports unit, API, UI, and mobile testing with built-in assertions, fixtures, and parameterization.
﷽
Background
Who has ever waited? Sometimes, waiting can be boring because we don’t know when what we’re waiting for will arrive. But nowadays, waiting is no longer dull, as we can still do some enjoyable activities while waiting, such as scrolling through social media (Instagram, Facebook, TikTok, etc.), watching our favorite movies on YouTube or Netflix, or even browsing and checking out items on online marketplaces.
Including myself—when waiting for something, I also do these things. However, on February 26, 2025, it was a bit different. While waiting for something, I decided to explore one of Python’s automation frameworks using my phone. I kept going back and forth reading the documentation and testing it directly in Termux . I explored Pytest, a very popular framework in Python that many companies use.
Let’s get started
Installation
To install Pytest, it’s very straightforward. If you’re using pip, you can use the following command:
pip install pytest
let’s try it out in my phone.
once you have pytest installed, you can try it out by running the following command:
pytest --version
as shown in the image above
Create a first test
again based on the documentation, let’s create a simple test. To do this, I’m using lazyvim
in my termux. Create file test file test_sample.py
with the following content:
# content of test_sample.py
1 def func(x):
2 return x + 1
3
4
5 def test_answer():
6 assert func(3) == 4
please note that on line 6, I use value 4
instead of 5
. So the test expected should be passed.
Run the test
To run the test, you can use the following command:
pytest
the command above will run all the tests in the current directory and its subdirectories, as mentioned in the documentation.
pytest
will run all files of the formtest_*.py
or*_test.py
in the current directory and its subdirectories
as you can see in the image above, I run the pytest
command twice. The first time, the test failed because the expected value is 4
but the actual value is 5
. The second time, the test passed because the expected value is 4
and the actual value is 4
as in the Create a first test.
Raise helper
Next, in the pytest documentation, I discovered something new: pytest can test whether a function throws a specific exception using the raises
helper. I gained this insight! 😁
Yes, in the pytest documentation you can take a look Assert that a certain exception is raised , here’s an example :
1 # content of test_sysexit.py
2 import pytest
3
4
5 def f():
6 raise SystemExit(1)
7
8
9 def test_mytest():
10 with pytest.raises(SystemExit):
11 f()
In lines 5-6, a function f()
is created. This function uses raise
to trigger an exception, specifically SystemExit(1).
In lines 9-10, a test function test_mytest()
is created. The test function uses pytest.raises(SystemExit)
to assert that calling f() raises a SystemExit exception
(line 11). Which means same exception (SystemExit). If f()
raises the expected exception, the test will pass. Otherwise, the test will fail.
and if we try to run the test, we will get the result as shown in the image below.
the green square indicates that the test passed, and the red square indicates that the test failed, because I made change function f()
to print("This an exception")
as image below.
Summary
Pytest is a very popular framework in Python that many companies use. It supports unit testing, API, UI, and mobile testing (with Appium both Android and iOS) with built-in assertions, fixtures, and parameterization.
That’s my notes.