Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test collection is not as expected when importing a static method from another test class #8500

Closed
4 tasks
uzibiton opened this issue Mar 30, 2021 · 2 comments
Closed
4 tasks
Labels
topic: collection related to the collection phase

Comments

@uzibiton
Copy link

uzibiton commented Mar 30, 2021

  • a detailed description of the bug or problem you are having
    I have two files - test_file1.py & test_file2.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 test_file1.py
    When running pytest on test_file1.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)) assert reinterpretation fails for some code layouts #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/allure_pytest/plugin.py
    pytest-env-0.6.2 at /usr/local/lib/python3.7/dist-packages/pytest_env/plugin.py
    pytest-ordering-0.6 at /usr/local/lib/python3.7/dist-packages/pytest_ordering/init.py
    pytest-timeout-1.4.2 at /usr/local/lib/python3.7/dist-packages/pytest_timeout.py
    pytest-repeat-0.8.0 at /usr/local/lib/python3.7/dist-packages/pytest_repeat.py
    container@docker-desktop:/home/ubuntu/workspace/test_qemu_container/hips-automation/sandbox/pytest_collect_issue$

  • minimal example if possible
    test_file2.py.txt
    test_file1.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 log_cli=true test_file1.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 ================================================================================================

test_file1.py::TestFile2::test_2_in_file2 & test_file1.py::TestFile2::test_1_in_file2 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.

@Zac-HD Zac-HD added the topic: collection related to the collection phase label Apr 1, 2021
@The-Compiler
Copy link
Member

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.

@uzibiton
Copy link
Author

uzibiton commented Apr 5, 2021

I understand. Thanks

@uzibiton uzibiton closed this as completed Apr 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: collection related to the collection phase
Projects
None yet
Development

No branches or pull requests

3 participants