Pytest Quick Start
Introduction
to
Pytest
Pytest Basics
• Testing Framework for Python
• API Testing
• Frontend (UI) Testing
• Unit Testing
• Simple or Complex
• Learn more: [Link]
Pytest Basics
• Finds tests automatically
• Test file names
ümust start with ‘test_*.py’ (_ required)
or
ümust end with ‘*_test.py’
• Finds files in current directory and subdirectories
Pytest Basics
• Test files can have just function for classes
• If class, then ‘__init__()’ should not exist
• Functions must start with ‘test*’ (_ not required)
def test_foo_bar(arg1, arg2, ..):
• Class names must start with ‘Test’ and methods with ‘test’
Class TestFooBar(object):
def test_verify_something():
PyTest Basics
# smoke_test.py # smoke_test.py
def test_user_login(): Class TestUserSmoke(object):
print(“log in”)
def test_user_login():
def test_user_register(): print(“log in”)
print(“log in”)
def test_user_register():
def test_user_logout(): print(“log in”)
print(“log in”)
def test_user_logout():
print(“log in”)
PyTest Basics
To run the test
$ pytest <options>
or
$ pytest <options> /path/to/test/files
or
$ [Link] <options> /path/to/test/filesc
or
$ python –m pytest <options> /path/to/test/files
Selecting Specific Tests
(Markers)
Selecting Tests by Markers
# smoke_test.py # smoke_test.py
import pytest import pytest
@[Link] @[Link]
def test_user_login(): Class TestUserSmoke(object):
print(“log in”)
def test_user_login():
@[Link] print(“log in”)
def test_user_register():
print(“register”) def test_user_register():
print(“register”)
Selecting Tests by Markers
# smoke_test.py # smoke_test.py
import pytest import pytest
pytestmark = [Link] pytestmark = [[Link],
[Link]]
def test_user_login():
Class TestUserSmoke(object):
print(“log in”)
def test_user_login():
def test_user_register():
print(“log in”)
print(“register”)
def test_user_register():
print(“register”)
Selecting Tests by Markers
# smoke_test.py
import pytest
@[Link]
@[Link]
def test_user_login():
print(“log in”)
def test_user_register():
print(“register”)
PyTest Basics
To run the test
$ pytest -m smoke
$ pytest -m “smoke or regression” ./tests
$ pytest -m “smoke and regression” ./tests
$ pytest -m “not prod” ./tests