Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 309
antony Avatar asked Apr 27 '12 08:04

antony


1 Answers

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

like image 163
hpk42 Avatar answered Oct 02 '22 16:10

hpk42