For bundling a pytest suite into a python cli tool or executable package for Docker. The issue is that pytest does not respect the folder directory when called using pytest.main.

Assuming a standard folder structure, create a for the entry point to call. Have the main entry point call this with the arguments you want. Then in pass the full directory path to pytest.main.

Now, you can bundle up this package into a wheel and distribute it as you wish. the user will be able to execute the package from any directory.

This is particularly useful for Web UI Automation tests or black-box tests where the tests are completely independent of the code.

Folder structure

- config/
- env/
- docker/
- jenkins/
- tests/
- should call this somewhere

automation.tests.main.run_tests(['-v', f'-m={marks}', f'--env={env}.yml'])


import os
import sys
import pytest

def run_tests(pytest_args):
    HERE = os.path.dirname(__file__)
    errcode = pytest.main(pytest_args)