Skip to content

PyTest 3.7 duplicating tests during collection #3854

@DanielSchiavini

Description

@DanielSchiavini

After upgrading to pytest 3.7, our projects suddently are collecting each test twice.
All tests run 2 times and the total percentage in the end of the run is between 33% and 55%.

I managed to create a small project to reproduce the issue:
testpytest.zip

Empty files:
./__init__.py
./lib/__init__.py
./lib/tests/__init__.py
./tests/__init__.py

Test files:
./lib/tests/TestPyTest.py
./tests/TestPyTest.py

from unittest import TestCase
class TestPyTest(TestCase):
    def test(self):
        pass

./pytest.ini

[pytest]
testpaths = .
python_files = Test*.py
python_classes = Test
python_functions = test

./Dockerfile

FROM python:3.7 AS base
WORKDIR /usr/src/app

FROM base AS pytest36
RUN pip install pytest==3.6.4
ADD . .
RUN ENV_NAME=testing pytest

FROM base AS pytest37
RUN pip install pytest==3.7
ADD . .
RUN ENV_NAME=testing pytest

Output of docker build .:

[...]
Step 4/10 : RUN pip install pytest==3.6.4
 ---> Using cache
 ---> 31c440fd1976
Step 5/10 : ADD . .
 ---> aa1d7f89c771
Step 6/10 : RUN ENV_NAME=testing pytest
 ---> Running in 72af6d63f895
============================= test session starts ==============================
platform linux -- Python 3.7.0, pytest-3.6.4, py-1.5.4, pluggy-0.7.1
rootdir: /usr/src/app, inifile: pytest.ini
collected 2 items

lib/tests/TestPyTest.py .                                                [ 50%]
tests/TestPyTest.py .                                                    [100%]

=========================== 2 passed in 0.04 seconds ===========================
Removing intermediate container 72af6d63f895
 ---> d843066e44ea
Step 7/10 : FROM base AS pytest37
 ---> 22d8eec60ce2
Step 8/10 : RUN pip install pytest==3.7
 ---> Using cache
 ---> d05ea16fa3f3
Step 9/10 : ADD . .
 ---> a8b18ec3669b
Step 10/10 : RUN ENV_NAME=testing pytest
 ---> Running in 9d5b48e1beb0
============================= test session starts ==============================
platform linux -- Python 3.7.0, pytest-3.7.0, py-1.5.4, pluggy-0.7.1
rootdir: /usr/src/app, inifile: pytest.ini
collected 3 items

lib/tests/TestPyTest.py ..                                               [ 33%]
tests/TestPyTest.py .

=========================== 3 passed in 0.04 seconds ===========================
Removing intermediate container 9d5b48e1beb0
 ---> 1affcfd67144
Successfully built 1affcfd67144
Successfully tagged pytest:latest

Note how pytest 3.6.4 correctly finds 2 tests, while pytest 3.7 collects 3 tests and reports a total of 33% run.

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: collectionrelated to the collection phasetype: bugproblem that needs to be addressedtype: regressionindicates a problem that was introduced in a release which was working previously

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions