Automation With Pytest

Wednesday, Feb 26, 2025 | 3 minute read | Updated at Thursday, Feb 27, 2025

Indra Sudirman
Automation With Pytest

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.

pytest-swtitching-app

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

pytest-docs-install

let’s try it out in my phone.

pytest-install-pip

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

simple-test-pytest

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 form test_*.py or *_test.py in the current directory and its subdirectories

pytest-run-test

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.

raises-pytest-sample-pass

and if we try to run the test, we will get the result as shown in the image below.

raises-run-terminal-passed-failed

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. updated-the-raises-test-exception

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.

© 2025 Blog Belajar Kode

🌱 Powered by Hugo with theme Dream.

About Me

I’m a software engineer wanna be, currently working as a software engineer at a startup company. I’m a quick learner and I’m always looking to improve my skills. I’m a team player and I’m always willing to help others.

Please feel free to contact me, though my curiculum vitae if you want to know more about me.