Hi@akhtar,
You need to create a file named test_*.py and add the below code to that file.
import math
def test_sqrt():
num = 25
assert math.sqrt(num) == 5
def testsquare():
num = 7
assert 7*7 == 40
def tesequality():
assert 10 == 11
Here I have created three functions and compare some conditions using assert keyword. Assert is the main keyword in Pytest code. It will validate your code and return True or False.
To test your code Pytest program, run Pytest command.
C:\Users\Nadeem Akhter\pytest>pytest -v
============================= test session starts =============================
platform win32 -- Python 3.6.10, pytest-3.10.1, py-1.8.2, pluggy-0.13.1 -- c:\users\nadeem akhter\anaconda3\envs\myenv\python.exe
cachedir: .pytest_cache
rootdir: C:\Users\Nadeem Akhter\pytest, inifile:
collected 2 items
test_code.py::test_sqrt PASSED [ 50%]
test_code.py::testsquare FAILED [100%]
================================== FAILURES ===================================
_________________________________ testsquare __________________________________
def testsquare():
num = 7
> assert 7*7 == 40
E assert (7 * 7) == 40
test_code.py:9: AssertionError
===================== 1 failed, 1 passed in 0.12 seconds ======================
I hope this will example will help you to understand the concepts.