pytest-dev / pytest-dev/pytest

`--pyargs` test discovery still fails in frozen application

Open
#14,771 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
14.5k
Forks
3.4k
Avg merge
2d 9h
Merged PRs (30d)
35

Description

I saw that #11511 is closed, to I tried it out, trying to package tests into a frozen app. Unfortunately, I now get

ERROR: module or package not found: myapp.test (missing __init__.py?)

pytest 9.1.1 with pyinstaller 6.21.0 on Windows 11.

MWE:

myapp\test\test_example.py

def test_example():
    assert 1 + 1 == 2

myapp\test_init_.py

myapp_init_.py

main.py

import sys

import pytest

if __name__ == "__main__":
    sys.exit(
        pytest.main(
            ["-o", "consider_namespace_packages=true", "--pyargs", "myapp.test"],
        )
    )

myapp.spec

from PyInstaller.building.api import COLLECT, EXE, PYZ
from PyInstaller.building.build_main import Analysis
from PyInstaller.utils.hooks import collect_submodules

a = Analysis(
    ["main.py"],
    hiddenimports=["myapp", "myapp.test", *collect_submodules("myapp.test"), "pytest"],
)
pyz = PYZ(a.pure)
exe = EXE(pyz, a.scripts, [], name="myapp")
coll = COLLECT(exe, a.binaries, a.datas, name="myapp")

pyproject.toml

[build-system]
requires = ["setuptools", "wheel"]
build-backend = "setuptools.build_meta"

[project]
name = "myapp"
version = "0.1.0"

[tool.pytest.ini_options]
consider_namespace_packages = true

run.bat

@echo off
pytest
python main.py
pyinstaller myapp.spec --clean -y
dist\myapp\myapp.exe

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the failure with main.py, myapp.spec, and run.bat using pytest 9.1.1 and PyInstaller 6.21.0 on Windows. Start at the --pyargs discovery path for the frozen executable and compare it with normal pytest discovery. Done means the packaged application locates myapp.test and runs test_example successfully.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing-qa
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.