pytest : All about pytest
Index :
- Installation x 2
- Project structure
- Sample code
- Important commands x 7
- Run specific test case
- Raise pytest exception (pytest.raises)
- labels (pytest.mark.label_name ,pytest.mark.skip,pytest.mark.skip_if)
- Parameterize (pytest.mark.parametrize('x,y,res',[(1,2,3),(0,3,3)]) )
- Fixture (pytest.fixture(scope="module") )
- conftest
- pdb
Installation:
Using pip
pip3 install pytest
pip install selenium
Using requirements.txt : project>requirements.txt
selenium
>= 3.0
pytest
>= 3.10.1
pytest-html
>= 1.20.0
pip install -r requirements.txt
or
pipenv install -r requirements.txt #Inside virtual env
Project structure
add file : - project>tests>test_filename.py
Sample Code
from selenium import webdriver
def test_1():
driver=webdriver.Chrome("chromedriverpath")
driver.get(url)
objs=driver.find_elements_by_xpath("//button")
for i in objs:
i.click()
-
open terminal
- cd to project
- excute command "pytest"
Important commands:
1.
pytest --pdb #runs the debugger when an err is encountered
2.
pytest -s #to "print" result in console
3.
pytest -v #verbose
4.
pytest --maxfail=2 # limit no of fails
5.
py.test --html=1.html
6.
pipenv run pytest
7.
pipenv run py.test --html=1.html
pytest -v -k "add or something" # runs only methods with add
,something
pytest -h # help
pytest -v -x #to stop after 1st fail
pytest -v -x --tb=no # do not display stack trace
pytest -q # quiet mode
pytest -rsx #report skipped tests
pytest --lf #runs only tests that failed on last attempt
# in eclipse Window > Preferences > Pydev> PyUnit =
Py.testRunner and add above Parameters
Execute specific method of class
pytest test_filename.py::test_method
Exception
def test_mytest():
with pytest.raises(ZeroDivisionError):
1/0
Markers(labels)
@pytest.mark.label_name" .To run "pytest -v -m
label_name"
@pytest.mark.skip(reason="any reason") #skip methods
@pytest.mark.skip_if(sys.version_info < (3,3),reason="some
reason")
Parametrize
import pytest
@pytest.mark.parametrize('x,y,res',[(1,2,3),(0,3,3)])
def test_add(x,y,res):
assert classname.add(x,y,res)
Fixtures(similar to @Before annotations in Junit test used for - db
connection , etc.,)
@pytest.fixture(scope="module") #scope =module/session/function
def fix():
pass #setup code
yield #run till here
print("done") # tear down code
def fn(fix):
#fix runs 1st
example:
from selenium import webdriver
@pytest.fixture()
def test_setup():
global driver
driver
=webdriver.Chrome(executable_path="C:/Users/driver.exe")# Windows
driver.implicitly.wait(5)
driver.maximise()
yield()
driver.close()
driver.quit()
conftest.py
You can put all fixtures inside this file and this file will execute before test starts
Debugger :
import pdb;
pdb.set_trace() # to
break
Others
Eclipse Setup :
(if u are using eclipse : Window->Preferences --> pydev --> PyUnit --> Change the Test runner to "Py.test runner".)
Right Click over the file. Run As --> Python Unit-Test
Or press Ctrl+F9:-
It will prompt you to select the test
User can use fixture or setup-teardown
setup
import pytest
def setup_module(module):
tiredown
import pytest
def teardown_module(module):
Allure report
pip install allure-pytest
pip list | grep -i allure
pytest --alluredir=/Users/Documents/reports
allure generate /Users/Documents/reports
setup.py
#Add this to setup.py file:
from setuptools import setup
setup(
# ...,
setup_requires=["pytest-runner", ...],
tests_require=["pytest", ...],
# ...,
)
#And create an alias into setup.cfg file:
[aliases]
test=pytest
[tool:pytest]
addopts = --verbose
python_files = testing/*/*.py
#If you now type:
python setup.py test
Links
#https://docs.pytest.org/en/latest/goodpractices.html#goodpractices
https://docs.pytest.org/en/latest/parametrize.html
https://docs.pytest.org/en/latest/example/parametrize.html#paramexamples
https://pypi.org/project/pytest-html/
https://pypi.org/project/pytest-csv/
important read
https://bytes.yingw787.com/posts/2018/12/10/data_driven_testing_three/
https://gitlab.com/qalabs/blog/pytest-parametrize-example
important watch **
https://www.youtube.com/watch?v=o9pEzgHorH0
https://www.youtube.com/watch?v=RdE-d_EhzmA
hooks
https://stackoverflow.com/questions/21930858/pytest-parameterize-row-from-csv-as-a-testcase?rq=1
Running pytest inside eclipse ide
Ref
:https://stackoverflow.com/questions/37985589/how-to-debug-or-run-pytest-scripts-using-eclipse