0% found this document useful (0 votes)
56 views

PyTest Quick Start

Pytest is a testing framework for Python that allows for automated testing of files containing test functions. Pytest finds test files automatically based on naming conventions and runs the test functions within. Markers can be used to select specific tests to run based on tags. The document provides details on pytest basics including file and test discovery, test structure requirements, and using markers to select tests.

Uploaded by

yanichik20
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

PyTest Quick Start

Pytest is a testing framework for Python that allows for automated testing of files containing test functions. Pytest finds test files automatically based on naming conventions and runs the test functions within. Markers can be used to select specific tests to run based on tags. The document provides details on pytest basics including file and test discovery, test structure requirements, and using markers to select tests.

Uploaded by

yanichik20
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Pytest Quick Start

Introduction
to
Pytest
Pytest Basics
• Testing Framework for Python

• API Testing

• Frontend (UI) Testing

• Unit Testing

• Simple or Complex

• Learn more: https://docs.pytest.org/en/latest/contents.html#toc


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
$ py.test <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

@pytest.mark.smoke @pytest.mark.smoke
def test_user_login(): Class TestUserSmoke(object):
print(“log in”)
def test_user_login():
@pytest.mark.regression 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 = pytest.mark.smoke pytestmark = [pytest.mark.smoke,


pytest.mark.fe]

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

@pytest.mark.smoke
@pytest.mark.fe
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

You might also like