Specific test files, directories, classes, or methods can be excluded using the nose-exclude plug-in for the nose. It has options --exclude-* .
To handle missing modules, you must install sys.modules with mock .
Fe, There's a Calc class in the mycalc module, but I don't have access to it because it is missing. And two more modules: mysuper_calc and mysuper_calc3 , the last one specific to Python 3. These two modules import mycalc and mysuper_calc3 should not be tested under Python 2. How can I guess them from the module in the text file? I guess this is the situation with OP.
calcareous / mysuper_calc3.py
from sys import version_info if version_info[0] != 3: raise Exception('Python 3 required') from mycalc import Calc class SuperCalc(Calc): '''This class implements an enhanced calculator ''' def __init__(self): Calc.__init__(self) def add(self, n, m): return Calc.add(self, n, m)
calcareous / mysuper_calc.py
from mycalc import Calc class SuperCalc(Calc): '''This class implements an enhanced calculator ''' def __init__(self): Calc.__init__(self) def add(self, n, m): return Calc.add(self, n, m)
Now, to make fun of mycalc ,
>>> from mock import Mock, patch >>> mock = Mock(name='mycalc')
The mycalc module has a Calc class that has an add method. I am testing the SuperCalc instance add method with 2+3 .
>>> mock.Calc.add.return_value = 5
Now the sys.modules and mysuper_calc3 can be conditionally imported into the with block.
>>> with patch.dict('sys.modules',{'mycalc': mock}): ... from mysuper_calc import SuperCalc ... if version_info[0] == 3: ... from mysuper_calc3 import SuperCalc
calc / doctest / mysuper_calc_doctest.txt
>>> from sys import version_info >>> from mock import Mock, patch >>> mock = Mock(name='mycalc') >>> mock.Calc.add.return_value = 5 >>> with patch.dict('sys.modules',{'mycalc': mock}): ... from mysuper_calc import SuperCalc ... if version_info[0] == 3: ... from mysuper_calc3 import SuperCalc >>> c = SuperCalc() >>> c.add(2,3) 5
The mysuper_calc_doctest.txt file must be alone in its own directory, otherwise nosetests will search for doctest in modules not tested.
PYTHONPATH=.. nosetests --with-doctest --doctest-extension=txt --verbosity=3
Doctest: mysuper_calc_doctest.txt ... ok
Ran 1 test at 0.038s
Ok
Python 3 detection wrapper around nosetests that passes .py files without syntax errors to nosetests
mynosetests.py
import sys from subprocess import Popen, PIPE from glob import glob f_list = [] py_files = glob('*py') try: py_files.remove(sys.argv[0]) except ValueError: pass for py_file in py_files: try: exec open(py_file) except SyntaxError: continue else: f_list.append(py_file) proc = Popen(['nosetests'] + sys.argv[1:] + f_list,stdout=PIPE, stderr=PIPE) print('%s\n%s' % proc.communicate()) sys.exit(proc.returncode)