pytest: Working














































pytest: Working



Python: pytest

pytest: Working

Creating unit tests using pytest is easy as we only need to create functions for the test. We can create a simple test function in just two lines of code.


Look at the below example, in which we have created a square method and run a unit test using the test() function.


Save the below code in the file named 'test1.py'.


def square(x):
return x**2


def test():
assert square(3) == 8


As we can see the assert statement is logically wrong as the value of 32 should be 9. We will now run the test using the below command.


pytest test1.py


The output of the above is


============================== test session starts ===============================
platform linux -- Python 3.8.5, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
rootdir: <file_path>
collected 1 item

test1.py F [100%]

================================= FAILURES ==================================
_____________________________________ test _______________________________________

def test():
> assert square(3) == 8
E assert 9 == 8
E + where 9 = square(3)

test1.py:6: AssertionError
============================ short test summary info =============================
FAILED test1.py::test - assert 9 == 8
=============================== 1 failed in 0.04s ================================


The test fails and the output shows the summary specifying why it failed. The platform used to run the tests and other info.

Now, we will correct the assertion statement.


Save the below code in file 'test2.py'.


def square(x):
return x**2


def test():
assert square(3) == 9


Now, we will run the test on this file using the below command.


pytest test2.py


The output is as follows


============================== test session starts ===============================
platform linux -- Python 3.8.5, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
rootdir: <file_path>
collected 1 item

test2.py . [100%]

=============================== 1 passed in 0.00s ===============================


The output shows that the test has passed.


We can also write multiple tests together by using a class whose methods represent different tests.


Save the below code in a file named 'test3.py'.


def square(x):
return x**2


class Test:
def test_one(self):
assert square(2) == 4

def test_two(self):
assert square(3) == 8

def test_three(self):
assert square(4) == 16


Again, we will run the tests using the below command.


pytest test3.py


The output is as follows


============================== test session starts ===============================
platform linux -- Python 3.8.5, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
rootdir: <file_path>
collected 3 items

test3.py .F. [100%]

================================= FAILURES ==================================
__________________________________ Test.test_two __________________________________

self = <test3.Test object at 0x7fe39eca2880>

def test_two(self):
> assert square(3) == 8
E assert 9 == 8
E + where 9 = square(3)

test3.py:10: AssertionError
============================ short test summary info =============================
FAILED test3.py::Test::test_two - assert 9 == 8
=========================== 1 failed, 2 passed in 0.05s ============================


The output shows which test cases passed and which failed, again with detailed info why they failed and how much time it took to run the tests.


In the similar way we can run any number of tests and check if the code we have is working as expected and if not what and where it is not showing expected results.



More Articles of Aniket Sharma:

Name Views Likes
Pyperclip: Installation and Working 990 2
Number Guessing Game using Python 683 2
Pyperclip: Not Implemented Error 1026 2
Hangman Game using Python 16785 2
Using Databases with CherryPy application 1672 2
nose: Working 506 2
pytest: Working 511 2
Open Source and Hacktoberfest 867 2
Managing Logs of CherryPy applications 1001 2
Top 20 Data Science Tools 684 2
Ajax application using CherryPy 799 2
REST application using CherryPy 663 2
On Screen Keyboard using Python 5508 2
Elastic Net Regression 815 2
US Presidential Election 2020 Prediction using Python 794 2
Sound Source Separation 1164 2
URLs with Parameters in CherryPy 1633 2
Testing CherryPy application 635 2
Handling HTML Forms with CherryPy 1448 2
Applications of Natural Language Processing in Businesses 508 2
NetworkX: Multigraphs 648 2
Tracking User Activity with CherryPy 1396 2
CherryPy: Handling Cookies 820 2
Introduction to NetworkX 633 2
TorchServe - Serving PyTorch Models 1301 2
Fake News Detection Model using Python 734 2
Keeping Home Routers secure while working remotely 483 2
Email Slicer using Python 2996 2
NetworkX: Creating a Graph 1108 2
Best Mathematics Courses for Machine Learning 551 2
Hello World in CherryPy 680 2
Building dependencies as Meson subprojects 978 2
Vehicle Detection System 1081 2
NetworkX: Examining and Removing Graph Elements 607 2
Handling URLs with CherryPy 536 2
PEP 8 - Guide to Beautiful Python Code 757 2
NetworkX: Drawing Graphs 624 2
Mad Libs Game using Python 643 2
Hosting Cherry applications 612 2
Top 5 Free Online IDEs of 2020 866 2
pytest: Introduction 534 2
Preventing Pwned and Reused Passwords 581 2
Contact Book using Python 2095 2
Introduction to CherryPy 547 2
nose: Introduction 505 2
Text-based Adventure Game using Python 3000 2
NetworkX: Adding Attributes 2278 2
NetworkX: Directed Graphs 1021 2
Dice Simulator using Python 560 2
Decorating CherryPy applications using CSS 833 2

Comments