Test collection is not as expected when importing a static method from another test class - Python pytest

  • [ ] a detailed description of the bug or problem you are having I have two files - testfile1.py & testfile2.py. In each of them a test class TestFile1 & TestFile2 and in each of the classes I have two tests. In class TestFile2 I have a static meth which I import to testfile1.py When running pytest on testfile1.py it collects 4 tests; two of them are correct and two are some storage combination.

  • [ ] output of pip list pip_list.txt ` from the virtual environment you are using

  • [ ] pytest and operating system versions cat /proc/version Linux version 5.4.72-microsoft-standard-WSL2 (oe-user@oe-host) (gcc version 8.2.0 (GCC)) #1 SMP Wed Oct 28 23:40:43 UTC 2020 pytest --version This is pytest version 5.2.4, imported from /usr/local/lib/python3.7/dist-packages/pytest.py setuptools registered plugins: allure-pytest-2.8.16 at /usr/local/lib/python3.7/dist-packages/allurepytest/plugin.py pytest-env-0.6.2 at /usr/local/lib/python3.7/dist-packages/pytestenv/plugin.py pytest-ordering-0.6 at /usr/local/lib/python3.7/dist-packages/pytestordering/init.py pytest-timeout-1.4.2 at /usr/local/lib/python3.7/dist-packages/pytesttimeout.py pytest-repeat-0.8.0 at /usr/local/lib/python3.7/dist-packages/pytestrepeat.py container@docker-desktop:/home/ubuntu/workspace/testqemucontainer/hips-automation/sandbox/pytestcollect_issue$

  • [ ] minimal example if possible testfile2.py.txt testfile1.py.txt

the content of test_file1.py:

import logging
from .test_file2 import TestFile2
class TestFile1(object):

    @staticmethod
    def method_in_file1():
        logging.warning("Running test {}".format("method_in_file1"))
        TestFile2.method_in_file2()

    def test_1_in_file1(self):
        logging.warning("Running test {}".format(self.__class__.__name__))

    def test_2_in_file1(self):
        logging.warning("Running test {}".format(self.__class__.__name__))

the content of test_file2.py:

import logging

class TestFile2(object):

    @staticmethod
    def method_in_file2():
        logging.warning("Running test {}".format("method_in_file2"))

    def test_1_in_file2(self):
        logging.warning("Running test {}".format(self.__class__.__name__))

    def test_2_in_file2(self):
        logging.warning("Running test {}".format(self.__class__.__name__))

Running the command pytest -o logcli=true testfile1.py The output

============================================================================================== test session starts ===============================================================================================
platform linux -- Python 3.7.3, pytest-5.2.4, py-1.9.0, pluggy-0.13.1
rootdir: /home/ubuntu/workspace/test_qemu_container/hips-automation/sandbox/pytest_collect_issue
plugins: allure-pytest-2.8.16, env-0.6.2, ordering-0.6, timeout-1.4.2, repeat-0.8.0
collected 4 items

test_file1.py::TestFile1::test_1_in_file1
------------------------------------------------------------------------------------------------- live log call --------------------------------------------------------------------------------------------------
WARNING  root:test_file1.py:13 Running test TestFile1
PASSED                                                                                                                                                                                                     [ 25%]
test_file1.py::TestFile1::test_2_in_file1
------------------------------------------------------------------------------------------------- live log call --------------------------------------------------------------------------------------------------
WARNING  root:test_file1.py:16 Running test TestFile1
PASSED                                                                                                                                                                                                     [ 50%]
test_file1.py::TestFile2::test_1_in_file2
------------------------------------------------------------------------------------------------- live log call --------------------------------------------------------------------------------------------------
WARNING  root:test_file2.py:11 Running test TestFile2
PASSED                                                                                                                                                                                                     [ 75%]
test_file1.py::TestFile2::test_2_in_file2
------------------------------------------------------------------------------------------------- live log call --------------------------------------------------------------------------------------------------
WARNING  root:test_file2.py:14 Running test TestFile2
PASSED                                                                                                                                                                                                     [100%]

=============================================================================================== 4 passed in 0.12s ================================================================================================

#

testfile1.py::TestFile2::test2infile2 & testfile1.py::TestFile2::test1infile2 should not be there I think if class TestFile2 had an autouse fixture it will also run it. Please let me know if I am doing something wrong. Thank you.

Asked Oct 11 '21 22:10
avatar uzibiton
uzibiton

1 Answer:

I don't see what's unexpected about this. You import TestFile2, so from pytest's point of view, it's just like the TestFile2 class was in test_file1.py as well.

1
Answered Apr 01 '21 at 13:38
avatar  of The-Compiler
The-Compiler