I am creating a library in python, which is to be compatible with both python2 and python3. I decided to use a feature that is available in python3 (namely the __prepare__
method on metaclass) and use a little hack (a counter on a class) for the same purpose in python2.
I have a PEP3115 constant defined simply as:
PEP3115 = sys.version_info >= (3, 0)
so I can use:
if PEP3115:
# python3 logic
else:
# python2 logic
Then if I add if PEP3115:
as ignored line to .coveragerc
i get the python3 logic ignored. However I also would like to create a different .coveragerc
file for python3 tests which will ignore python2 logic but count python3 one. Can I do it with the above code, or do I need to modify it?
There's no need to ignore either branch of your if/else. The best thing to do is to run your test suite twice, once under Python 2, then again under Python 3, but use different data files for each run. Then use "coverage combine" to combine the data from the two runs, then report from that data. The resulting report will show both halves of the if/else being executed.