Basic Pytest Test
Here we are going to write a basic test. Pytest looks for tests in files named starting with ‘test_’ or ending with ‘_test.py’. We create a function reverse_text() that takes string input and return its reverse. After that we create another function test_reverse_test() which tests the function reverse_text(). Inside test_reverse_test() we assert that output of reverse_text(‘python’) is equal to ‘nohtyp’. If this is true, the test passes. If not, Pytest flags this is a failed test.
# test_reversal.py
def reverse_text(text):
return text[::-1]
def test_reverse_text():
assert reverse_text('python') == 'nohtyp'
Output: To run the above test we have to execute “pytest” in the terminal and we will get the below output.
Pytest Tutorial | Unit Testing in Python using Pytest Framework
In the process of development of applications, testing plays a crucial role. It ensures the developers that the application is error-free and the application is running as per the expectation. It should be done to ensure a seamless experience for the user. In this article, we will learn about testing Python applications using Pytest.
Contact Us