Loader: Test Classes

Load tests from classes that are not unittest.TestCase subclasses.

This plugin responds to loadTestsFromModule() by adding test cases for test methods found in classes in the module that are not sublcasses of unittest.TestCase, but whose names (lowercased) match the configured test method prefix.

Test class methods that are generators or have param lists are not loaded here, but by the nose2.plugins.loader.generators.Generators and nose2.plugins.loader.parameters.Parameters plugins.

This plugin also implements loadTestsFromName() to enable loading tests from dotted class and method names passed on the command line.

This plugin makes two additional plugin hooks available for other test loaders to use:

nose2.plugins.loader.testclasses.loadTestsFromTestClass(self, event)
Parameters:event – A LoadFromTestClassEvent instance

Plugins can use this hook to load tests from a class that is not a unittest.TestCase subclass. To prevent other plugins from loading tests from the test class, set event.handled to True and return a test suite. Plugins can also append tests to event.extraTests. Ususally, that’s what you want, since it allows other plugins to load their tests from the test case as well.

nose2.plugins.loader.testclasses.getTestMethodNames(self, event)
Parameters:event – A GetTestMethodNamesEvent instance

Plugins can use this hook to limit or extend the list of test case names that will be loaded from a class that is not a unittest.TestCase subclass by the standard nose2 test loader plugins (and other plugins that respect the results of the hook). To force a specific list of names, set event.handled to True and return a list: this exact list will be the only test case names loaded from the test case. Plugins can also extend the list of names by appending test names to event.extraNames, and exclude names by appending test names to event.excludedNames.

About Test Classes

Test classes are classes that look test-like but are not subclasses of unittest.TestCase. Test classes support all of the same test types and fixtures as test cases.

To “look test-like” a class must have a name that, lowercased, matches the configured test method prefix – “test” by default. Test classes must also be able to be instantiated without arguments.

What are they useful for? Mostly the case where a test class can’t for some reason subclass unittest.TestCase. Otherwise, test class tests and test cases are functionally equivalent in nose2, and test cases have broader support and all of those helpful assert* methods – so when in doubt, you should use a unittest.TestCase.

Here’s an example of a test class:

class TestSomething(object):

    def test(self):
        assert self.something(), "Something failed!"

Configuration [test-classes]


Sample configuration

The default configuration is equivalent to including the following in a unittest.cfg file.

always-on = True

Plugin class reference: TestClassLoader

class nose2.plugins.loader.testclasses.TestClassLoader[source]

Loader plugin that loads test functions


Load test classes from event.module


Load tests from event.name if it names a test class/method


Install extra hooks

Adds the new plugin hooks:

  • loadTestsFromTestClass
  • getTestMethodNames