Search code examples
pythonnosenosetests

run some tests only in specific environment


I am writing some tests which only make sense in a specific environment, e.g. if the host has a Nvidia CUDA capable GPU, or maybe it depends on other resources which are specific to some environment.

How can I specify that with Nosetests? My optimum would be if Nosetests would list the skipped tests and the reason for the skips.


Solution

  • You can do it generally with standard unittest library. At the very start of your tests you can always check for prerequisites with decorators (preferred) or by raising SkipTest exception.

    import unittest 
    
    class MyTestCase(unittest.TestCase):
    
        @unittest.skipIf(not have_gpu(),
                         "no gpu on this system")
        def test_gpu_performance(self):
            pass
    
        def test_something_else(self):
            if not have_gpu():
                raise unittest.SkipTest("no gpu")