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

session scope fixtures can't run tests which does not start with test* or setup* #948

Closed
mrniranjan opened this issue Aug 19, 2015 · 6 comments

Comments

@mrniranjan
Copy link

I am following the link: https://pytest.org/latest/example/special.html to understand how session scoped fixtures work if we have to call a particular test function like 'callme' as specified in the example

But the example doesn't work with pytest version 2.7, (works as expected with pytest 2.3).

Below is the conftest.py

import pytest

@pytest.fixture(scope="session", autouse=True)
def callattr_ahead_of_alltests(request):
    print ("callattr_ahead_of_alltests called")
    seen = set([None])
    session = request.node
    for item in session.items:
        cls = item.getparent(pytest.Class)
        if cls not in seen:
            if hasattr(cls.obj, "callme"):
               cls.obj.callme()
            seen.add(cls)

Below is the test_module.py

class TestHello:
    @classmethod
    def callme(cls):
        print ("callme called!")

    def test_method1(self):
        print ("test_method1 called")

    def test_method2(self):
        print ("test_method1 called")

class TestOther:
    @classmethod
    def callme(cls):
        print ("callme other called")
    def test_other(self):
        print ("test other")

# works with unittest as well ...
import unittest

class SomeTest(unittest.TestCase):
    @classmethod
    def callme(self):
        print ("SomeTest callme called")

    def test_unit1(self):
        print ("test_unit1 method called")``

Output when run:

$ py.test test_module.py -s
========================== test session starts ========================== 
platform linux2 -- Python 2.7.5 -- py-1.4.27 -- pytest-2.7.0
rootdir: /root, inifile: pytest.ini
plugins: multihost
collected 1 items 

test_module.py callattr_ahead_of_alltests called
SomeTest callme called
test_unit1 method called
.

As you can see in the above output, callme function is called only if unittest.testcase module is loaded otherwise this function is not called.

Where as with py.test 2.3 it works as expected.

platform linux2 -- Python 2.7.5 -- pytest-2.3.5
collected 4 items 

test_module.py callattr_ahead_of_alltests called
callme called!
callme other called
SomeTest callme called
test_method1 called
.test_method1 called
.test other
.test_unit1 method called
.

Additional Details:
Version of python: python-2.7.5-16.el7.x86_64 (In both working and non working systems)
RHEL7.1 (x86_64)

@mrniranjan
Copy link
Author

Any update or comments on the above. ?

@nicoddemus
Copy link
Member

Hi, sorry for taking so long to respond! 😅

Hmmm I can't reproduce your problem. I copied your examples verbatim, and this is the output I get against different pytest versions:

pytest-2.7.0

============================= test session starts =============================
platform win32 -- Python 2.7.10 -- py-1.4.30 -- pytest-2.7.0
rootdir: X:\pytest, inifile: tox.ini
collected 4 items

test_module.py callattr_ahead_of_alltests called
callme called!
callme other called
SomeTest callme called
test_method1 called
.test_method1 called
.test other
.test_unit1 method called
.

========================== 4 passed in 0.02 seconds ===========================

pytest-2.7.2

============================= test session starts =============================
platform win32 -- Python 2.7.10 -- py-1.4.30 -- pytest-2.7.2
rootdir: X:\pytest, inifile: tox.ini
collected 4 items

test_module.py callattr_ahead_of_alltests called
callme called!
callme other called
SomeTest callme called
test_method1 called
.test_method1 called
.test other
.test_unit1 method called
.

========================== 4 passed in 0.02 seconds ===========================

pytest-2.8.0.dev4

============================= test session starts =============================
platform win32 -- Python 2.7.10, pytest-2.8.0.dev4, py-1.4.30, pluggy-0.3.0
rootdir: X:\pytest, inifile: tox.ini
collected 4 items

test_module.py callattr_ahead_of_alltests called
callme called!
callme other called
SomeTest callme called
test_method1 called
.test_method1 called
.test other
.test_unit1 method called
.

========================== 4 passed in 0.02 seconds ===========================

Of course I tried it on Windows, but this is kind of issue is hardly platform specific...

@nicoddemus
Copy link
Member

Just tried it on a Ubuntu box too, same results for pytest-2.7.0 and pytest-2.7.2.

@nicoddemus
Copy link
Member

@mrniranjan could you perhaps try your example in a clean virtualenv?

@nicoddemus
Copy link
Member

@mrniranjan any news?

@mrniranjan
Copy link
Author

I apologise for the delay,

I tested again in both rhel7.2(beta) and Fedora-21 to see if i can reproduce again,(without virtualenv, as my original reported issue was without virtualenv)
On RHEL7.2
pytest-2.6.4-1.el7ost.noarch

platform linux2 -- Python 2.7.5 -- py-1.4.27 -- pytest-2.6.4
collected 3 items 

test1.py callattr_ahead_of_alltests called
callme called!
callme other called
test_method1 called
.test_method1 called
.test other

On Fedora 21. (pytest-2.6.4-1.fc21.noarch)

platform linux2 -- Python 2.7.8 -- py-1.4.30 -- pytest-2.6.4
plugins: multihost
collected 3 items 

test1.py callattr_ahead_of_alltests called
callme called!
callme other called
test_method1 called
.test_method1 called
.test other
.

In both the cases below is the test1.py and conftest.py

class TestHello:
    @classmethod
      def callme(cls):
           print ("callme called!")

      def test_method1(self):
           print ("test_method1 called")

      def test_method2(self):
          print ("test_method1 called")
class TestOther:
     @classmethod
      def callme(cls):
           print ("callme other called")
     def test_other(self):
          print ("test other")

As you can see, I don't see the issue again, I am still not sure, why the issue was seen only in one system. This issue can be closed now,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants