Skip to content

jeecha/pytest-intro

Repository files navigation

Installation

Install python 3.x from www.python.org

When installing, select "Add Python x.x to PATH", so python, pip and other Python related binaries are accessible from the shell.

Verify installation by launching shell and running python command.

Install pytest

If Python is properly installed and available in shell, pip can be used to install pytest:

pip install pytest

Verify installation:

pytest --version

Hello world

Pytest invocation

  • verbose output - pytest -v
  • stopping after failures
    • stop after first failure - pytest -x, pytest --maxfail=1
    • stop after first N failures - pytest --maxfail=N
  • selecting tests to be run
    • default test look-up
    • run all tests in a Python module - pytest test_helloworld.py
    • run specific tests by specifying method or class
      • run specific test in the module - pytest test_run_specific_test::test_pass
      • run all tests in the class in the module - pytest test_run_specific_test.py::TestSomething
      • run specific test in the class in the module - pytest test_run_specific_test::TestSomething::test_something
    • filter by keywords
      • test name contains string - pytest -k pass
      • boolean operators: and, or, not etc - pytest -k "not fail"
    • filter by marks
  • show stdout
    • automatically captured for failed tests - pytest test_stdout_fail.py
    • can be shown for passed tests - pytest -s test_stdout_pass.py

Test result

Marks

Test parametrization

Fixtures

  • pytest.fixture
  • fixture finalization
  • fixture scope
  • access to test context
  • fixture parametrization
  • fixtures that use other fixtures
  • pytest.mark.usefixtures
  • pytest.fixture(autouse=True)
  • built-in fixtures
  • excercise - fixture_excercise/test.py

conftest.py

Allure integration

Installation of Pytest plug-in for Allure

pip install allure-pytest

Installation of Allure

Generating Allure input from Pytest test execution

pytest --aluredir=/path/to/allure/input

Creating Allure test report

Unix:

allure serve /path/to/allure/input

Windows;

allure.bat serve /path/to/allure/input

Writing tests with Allure in mind

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages