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:
- Run the pytest from command prompt
- Another way of writing pytest
- Running multiple pytests
- Creating and running multiple tests inside same module
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.
By default, the parameter set for test runner is ‘—verbosity 0’
Let us change the parameter to –vv as seen below
Apply and close
Now when we run the pytest, we see additional information about the test viz “test_case_one PASSED”
Similarly, we see additional information about the test viz “test_case_one FAILED”, see below
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)
Run the pytest from command prompt
Let us try to run the below pytest using command prompt
To do that, let us find out the path of ‘PytestPackage’
Copy the path from below
Go to command prompt & cd to this path
Type ‘pytest’, see below
Hit enter, the pytest starts executing
So see below, the test is seen as passed
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
Running multiple pytests
Select entire code & copy the code
Create another module test_dummy & paste the code
Go to command prompt & run ‘pytest’ command. Notice that both the tests are executed & get passed
Let us rename test_dummy.py to dummy.py
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.
Creating and running multiple tests inside same module
Copy the first method & paste it. Rename the second method, see below
Run pytest. See that 2 tests are executed.
The 2 browsers open for the 2 methods (tests).
We read lot of topics around pytest & will continue more in our next article. Thank You for reading!