简体   繁体   中英

py.test running Python2 and Python3

I have written a package ( http://github.com/anntzer/parsedcmd ) that runs with both Python2 and Python3. However, I had to write separate (py.test) unit tests for Python2 and Python3 (mainly because I want to test extra features of Python3, in particular keyword-only arguments), so I have a test_py2.py and a test_py3.py in a test subpackage. Now, if I run, say py.test2 mypkg , test_py2 passes, but test_py3 fails with a SyntaxError . Likewise, for py.test3 mypkg , test_py3 passes but test_py2 fails (I could make this one work though, it's just an issue of StringIO having moved to io ).

I can design the test subpackage so that import mypkg.test only imports the proper version of the tests, but apparently py.test doesn't care -- it just sees two files matching test_* and grabs all tests in both of them, ignoring what __init__.py tells him to import.

So right now I have to do both py.test2 mypkg/test/test_py2.py and py.test3 mypkg/test/test_py3.py . Is there a way to set up the whole thing so that py.test2 mypkg and py.test3 mypkg would "just work"?

Thanks.

If you can then making your modules importable on all interpreters and skipping tests as appropriate is a common solution. Otherwise you can put the following as "conftest.py" into the test directory:

import sys
py3 = sys.version_info[0] >= 3

class DummyCollector(pytest.collect.File):
   def collect(self):
      return []

def pytest_pycollect_makemodule(path, parent):
  bn = path.basename 
  if "py3" in bn and not py3 or ("py2" in bn and py3):
     return DummyCollector(path, parent=parent)

This gets picked up a project-specific plugin and will properly ignore a test module with a filename containing a "py2" or "py3" substring on the wrong interpreter version. Of course you can refine it to rather have an explicit list directly in the conftest.py file instead of checking the filename etc.pp.

HTH, holger

You can put your tests in different packages and run only the tests in the appropriate package. Or you can load the appropriate test module in a script:

import sys, unittest

cur_version = sys.version_info

if cur_version[0] < 3:
   import myApp.test.test_py2
   unittest.TestLoader().loadTestsFromModule(myApp.test.test_py2).run()
else:
   import myApp.test.test_py3
   unittest.TestLoader().loadTestsFromModule(myApp.test.test_py3).run()

Alternatively, use a setup.py file so you can run:

python setup.py test

and put the versioning logic in there:

versionedTestSuite = "parsedcmd.test.test_py2" # do something as above here
setup(name='parsedcmd',
      ...
      test_suite=versionedTestSuite,
      )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM