Skip to main content

Control your tests flow

Project description

codecov

pytest-directives

Control your tests flow.

Provides directives, that makes process of running tests clear and controllable.

Install:

pip install pytest-directives

Example:

Run your test

  1. Create test with pytest
# test_something.py

def test_something():
    assert False, 'Some product error'
  1. Describe your flow
# simple_flow.py

import asyncio
import logging

from pytest_directives import sequence

import test_something


simple_flow = sequence(test_something, )

if __name__ == '__main__':
    logging.basicConfig(level=logging.DEBUG, format='%(message)s',)
    asyncio.run(
        simple_flow.run()
    )
  1. Run as usual python code python simple_flow.py
Using proactor: IocpProactor
Run test from directive PytestRunnable: C:\Users\i.kuzmenko\PycharmProjects\LOCAL\pytest-directives\test_something.py
============================= test session starts =============================
platform win32 -- Python 3.12.10, pytest-8.4.1, pluggy-1.6.0 -- C:\Users\i.kuzmenko\PycharmProjects\LOCAL\pytest-directives\.venv\Scripts\python.exe
rootdir: C:\Users\i.kuzmenko\PycharmProjects\LOCAL\pytest-directives
configfile: pyproject.toml
plugins: asyncio-1.1.0, cov-6.2.1
asyncio: mode=Mode.AUTO, asyncio_default_fixture_loop_scope=function, asyncio_default_test_loop_scope=function
collecting ... collected 1 item

test_something.py::test_something FAILED                                 [100%]

================================== FAILURES ===================================
_______________________________ test_something ________________________________

    def test_something():
>       assert False, 'Some product error'
E       AssertionError: Some product error
E       assert False


test_something.py:3: AssertionError
=========================== short test summary info ===========================
FAILED test_something.py::test_something - AssertionError: Some product error
============================== 1 failed in 0.07s ==============================
Errors in tests results

Three main directives

from pytest_directives import sequence, chain, parallel

sequence()  # run items one by one; ignore errors

chain()     # run items one by one; stop when first fail found

parallel()  # run items in parallel; ignore errors

How to run directives

from pytest_directives import sequence

# just import your tests
from tests import test_file_1, test_file_2, test_file_3


# define order in `flow`
sequence_flow = sequence(
    test_file_1,
    test_file_2,
    test_file_3
)

# add some options to pytest
pytest_run_args = (
    "-v",
    "--alluredir=./allure-results",
)

import asyncio
# start your flow
asyncio.run(
    sequence_flow.run(*pytest_run_args)
)

Combine directives in flows

from pytest_directives import sequence, chain, parallel


abstract_flow = sequence(
    chain(
        chain(
            # prepare infrastructure or tests environment
        ),
        # if infrastructure failed, exit from here
        parallel(
            # run tests in parallel to increase speed of testing
        )
    ),
    sequence(
        # do some important stuff at the end of tests,
        #   like collect logs, metrics or just cleanup environment
    )
)

smoke_flow = sequence(
    chain(
        chain(
            prepare_environment(),
            check_infrastructure_health()
        ),
        parallel(
            run_test_group_a(),
            run_test_group_B(),
            run_api_tests()
        ),
    ),
    sequence(
        collect_logs(),
        generate_report(),
        cleanup_environment()
    )
)

Features

  • Can run tests by import package, module, function, class or method
  • Run pytest in separate process (say no to sharing fixture) by asyncio.create_subprocess_exec
  • Can use pytest.marks / pytest-xdist or other as usual by using .run(*args) parameter
  • Combine directives and implement your tests flow as you need

Development

  1. Install uv
  2. Clone project
  3. Install requirements
  uv sync

Run tests

  inv tests

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest_directives-0.0.3.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_directives-0.0.3-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

File details

Details for the file pytest_directives-0.0.3.tar.gz.

File metadata

  • Download URL: pytest_directives-0.0.3.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.8

File hashes

Hashes for pytest_directives-0.0.3.tar.gz
Algorithm Hash digest
SHA256 f08890cc9e6484cb87261545d804c544011a58d047816c2ba813ab09429da5d9
MD5 57d0c58172556e0ee94f17b1fe4c7713
BLAKE2b-256 e49b8aa3e9e75e213599d35a5dda544a222904c67d9805f1ff62509b5ccc274e

See more details on using hashes here.

File details

Details for the file pytest_directives-0.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_directives-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 aafe0e5144e2b778de421de71e8c879e3210e24fc86562f81f42a57c5a32db5e
MD5 a97662ae4e4baf63d2ed67a5ca501b3e
BLAKE2b-256 aafe51c055f1ac49943e11d683e0c1ed388032cbd9eac9a658a3b5471d79492b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page