implicit loading of sibling directory's conftest.py

Issue #109 resolved
Former user created an issue

As discussed on the mailing list (subject: funcarg name clash / conftest load behaviour) it would be nice if py.test does not implicitely load the sibling directory's conftest.py in case those directories are not packaged with an init.py.

Consider the directory structure:

ROOTDIR/ a/ conftest.py test_hello.py b/ conftest.py test_hello.py

Calling py.test from ROOTDIR with argument 'a' (or from ROOTDIR/a without args) should not load b/conftest.py.

The applied patch against py-1.3.1 adds a test case for this situation. I have to say that I'm not sure whether the location is best and things in it might be done better, because this was the fist time I'm in touch with the code.

Best regards, Frank

Comments (6)

  1. Anonymous
    diff -aur py-1.3.1/testing/test_collect.py py-1.3.1-first-non-init-containing-dir-test/testing/test_collect.py
    --- py-1.3.1/testing/test_collect.py	2010-05-25 20:16:40.000000000 +0200
    +++ py-1.3.1-first-non-init-containing-dir-test/testing/test_collect.py	2010-06-27 13:41:45.000000000 +0200
    @@ -221,7 +221,28 @@
             col = config.getnode(x)
             trail = config._rootcol.totrail(col)
             col2 = config._rootcol.fromtrail(trail)
    -        assert col2 == col 
    +        assert col2 == col
    +        
    +    def test_first_non_init_containing_dir(self, testdir):
    +        for dirname in ('a', 'b'):
    +            testdir.tmpdir.ensure(dirname, dir=True)
    +            testdir.tmpdir.ensure(dirname, '__init__.py')
    +            
    +            # To create the conftest.py I would like to use testdir.make*-methods
    +            # but as far as I have seen they can only create files in testdir.tempdir
    +            backupTmpDir = testdir.tmpdir
    +            testdir.tmpdir = testdir.tmpdir.join(dirname)
    +            testdir.makeconftest("""
    +                _DIR_NAME = '%s'
    +                def pytest_configure(config):
    +                    if config.args and config.args[0] != _DIR_NAME:
    +                        raise Exception("py.test run for '" + config.args[0] + "', but '" + _DIR_NAME + "/conftest.py' loaded.")
    +            """ % dirname)
    +            testdir.tmpdir = backupTmpDir
    +            
    +        for dirname, other_dirname in (('a', 'b'), ('b', 'a')):
    +            result = testdir.runpytest(dirname)
    +            assert result.ret == 0, "test_first_non_init_containing_dir: py.test run for '%s', but '%s/conftest.py' loaded." % (dirname, other_dirname)
            
         def test_totrail_topdir_and_beyond(self, testdir, tmpdir):
             config = testdir.reparseconfig()
    
  2. struppimoppi

    Please ignore the patch from the last anonymous comment. Now I managed to sign up to bitbucket and am able to attach files... :) I reviewed the patch so the attached file will work for py-1.3.2 and I added a little more comments too.

    Best Regards, Frank

  3. Bluebird

    Is there any way to disable the sibling directory looking ? I read the discussion on the ML and I entirely agree with Frank. Test behavior should not depend on side directories. I may have up to 6 or 7 checkout of the same project in one directory. Running py.test in one of them runs it in all of them !

  4. Log in to comment