Tutorial 17 – Pytest Features

Welcome to the seventeenth tutorial of Selenium-Python series! We will continue learning pytest features. Please read previous tutorial before reading this one.

What you will Learn:
  • Verbosity
  • Run the pytest from command prompt
  • Another way of writing pytest
  • Running multiple pytests
  • Creating and running multiple tests inside same module
  • Conclusion

Verbosity
Let us re-run the pytest that we had created in our previous article. We see a message “PytestPackage\test_firsttest.py” in the console window.

Figure 1

By default, the parameter set for test runner is ‘—verbosity 0’

Figure 2

Let us change the parameter to –vv as seen below


Figure 3

Apply and close

Now when we run the pytest, we see additional information about the test viz “test_case_one PASSED”


Figure 4

Similarly, we see additional information about the test viz “test_case_one FAILED”, see below


Figure 5

So we get to know the name of the test case that is passed or failed.

We can also mention the chromedriver path in ‘path’ variable (line#4 below). We can than pass this ‘path’ in constructor (line 5)


Figure 6

Run the pytest from command prompt
Let us try to run the below pytest using command prompt

Figure 7

To do that, let us find out the path of ‘PytestPackage’

Figure 8

Copy the path from below


Figure 9

Go to command prompt & cd to this path


Figure 10

Type ‘pytest’, see below


Figure 11

Hit enter, the pytest starts executing


Figure 12

So see below, the test is seen as passed


Figure 13

Another way of writing pytest
Comment line#1 & write line#2. Comment line#6 & write line#7. This is another way to write the same pytest


Figure 14



Figure 15


Figure 16

Running multiple pytests
Select entire code & copy the code

Figure 17

Create another module test_dummy & paste the code

Figure 18

Go to command prompt & run ‘pytest’ command. Notice that both the tests are executed & get passed

Figure 19

Let us rename test_dummy.py to dummy.py

Figure 20


Figure 21


Figure 22

So we now have one pyest that does not have ‘test’ prefix. Now when we run pytest command, see below that only 1 pytest gets executed. The other test that does not have ‘test’ prefix is not executed.

Figure 23

Creating and running multiple tests inside same module
Copy the first method & paste it. Rename the second method, see below


Figure 24

Run pytest. See that 2 tests are executed.


Figure 25

The 2 browsers open for the 2 methods (tests).


Figure 26

Conclusion
We read lot of topics around pytest & will continue more in our next article. Thank You for reading!