Handouts Pytest
Handouts Pytest
About Pytest:
Test framework based on Python language
Easy to write, execute and generate test reports
Different types and levels of testing
Used by developers and QA team
Install Pkgs & Test Naming
• Test Discovery:
• Given no arguments, pytest looks at your current directory and all subdirectories for test files and runs the test
code it finds.
• If you give pytest a filename, a directory name, or a list of those, it looks there instead of the current directory.
• Each directory listed on the command line is recursively traversed to look for test code.
Pytest Markers
• Markers:
• Use markers for grouping our tests.
• A test can have more than one marker, and a marker can be on multiple tests.
• E.g. marking a test:
• @pytest.mark.sanity, @pytest.mark.uitest, etc
• While running tests, use pytest -m sanity, to run only test marked with "sanity"
• Markers running supports: and, or, not, parentheses operators.
• E.g. pytest -m "sanity and uitest"
• Can define markers in module/file level:
• pytestmark=[pytest.mark.smoke, pytest.mark.sanity]
• For stopping the warnings, need add/define the markers in pytest.ini in root folder
Pytest Cmd Line options
• Cmd line options:
• See all options with pytest --help.
• Few of them below:
• -m MARKEXPR only run tests matching given mark expression.
• -x, --exitfirst, exit instantly on first error or failed test.
• --tb=no, no traceback
• --maxfail=num exit after first num failures or errors.
• -q, --quiet decrease verbosity.
• --collect-only only collect tests, don't execute them.
• --version display pytest lib version and import information.
Parameterize & Fixtures
• Parametrized Tests:
• Parameterized tests allow a developer to run the same test over and over again on different values.
• Parameterized testing is when we want to do data driven testing. For example, we want to test the login page or process, with multiple
input values for e.g. say 1000 different username and password conditions.
• This lets you increase the coverage without having to add much more test code.
• E.g. @pytest.mark.parametrize("test_input,expected", [("5+5", 10), ("5-5", 0), ("7*8", 56)])
• Fixtures:
• Fixtures is used to get data ready for our tests. Fixtures are functions that are run by pytest before (and sometimes after) the actual
test functions.
• We can use fixtures to get a data set for the tests to work on. For e.g. you want to setup DB connection, or initialize webdriver and test
browser if talking in terms of selenium UI tests.
• We can put fixtures in individual test files if we want the fixture to only be used by tests in that file.
• Proper way of use is to have fixtures in conftest.py to be used by multiple test files.
• Fixture functions can have a any name.
• Call fixture by using in test-function argument. e.g. def test_someTest(fixture_name)
• Mark fixture using @pytest.fixture()
• Its not mandatory but we can return something from fixture using return statement.
Parameterize & Fixtures
• Fixtures:
• Also another way of calling fixture:
• @pytest.mark.usefixtures("fixture_name")
• In this case, return from fixture cannot be used. So only use it to execute some code within fixture.
• Sharing Fixtures:
• Use conftest.py - put the fixtures to share across multiple test files.
• We can have more conftest.py files in subdirectories of the top tests directory. If we do, fixtures defined in these lower-level conftest.py
files will be available to tests in that directory and subdirectories.
• Don’t import conftest from anywhere. The conftest.py file gets read by pytest, and is considered a local plugin.
• Function- pytest_configure, vars within this are available within all test functions in that module.