How to create one Pytest program

0 votes

Hi Guys,

I am new in the Pytest module. Can anyone guide me on how I can create one Pytest program?

Jun 24, 2020 in Python by akhtar
• 38,260 points
778 views

1 answer to this question.

0 votes

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.

answered Jun 24, 2020 by MD
• 95,460 points

Related Questions In Python

0 votes
1 answer

How to create a train and test sample from one dataframe using pandas?

Hi, The below written code can help you ...READ MORE

answered Jul 4, 2019 in Python by Taj
• 1,080 points
5,756 views
+1 vote
1 answer

How to create plots using python matplotlib in IPython notebook?

I think you should try: I used %matplotlib inline in ...READ MORE

answered Aug 8, 2018 in Python by Priyaj
• 58,020 points
1,616 views
+1 vote
3 answers

How to run the Python program forever?

you can also do the old fashioned ...READ MORE

answered Jun 10, 2019 in Python by brianno
21,443 views
0 votes
2 answers

How to create new folder?

import os try: os.makedirs(path) except ...READ MORE

answered Aug 31, 2018 in Python by Omkar
• 69,220 points
860 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,464 views
0 votes
1 answer
0 votes
1 answer

How to test python code using Pytest?

Hi@akhtar, You can use the Pytest module to ...READ MORE

answered Jun 24, 2020 in Python by MD
• 95,460 points
1,046 views
0 votes
1 answer

How to create Pytest report in html format?

Hi@akhtar, You can create a Pytest report in ...READ MORE

answered Jun 24, 2020 in Python by MD
• 95,460 points
5,176 views
0 votes
1 answer

How to create new environment using conda?

Hi@akhtar, Environment means you are creating your own ...READ MORE

answered Apr 21, 2020 in Python by MD
• 95,460 points
1,142 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP