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 main.py for the entry point to call. Have the main entry point call this main.py with the arguments you want. Then in main.py 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

Package/
- config/
- env/
- docker/
- jenkins/
- tests/
  - __init__.py
  - main.py
  - conftest.py
  - test1.py
  - test2.py
- __init__.py
- setup.py

init.py should call this somewhere

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

tests/main.py

import os
import sys
import pytest


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