Get data structure of all tests found by Nose
Question: How would I get some sort of data structure containing a list of all tests
found by Nose? I came across this:

[List all Tests Found by
Nosetest](http://stackoverflow.com/questions/712020/list-all-tests-found-by-
nosetest)

I'm looking for a way to get a list of unit test names in a python script of
my own (along with the location or dotted location).


Answer: There are several ways to achieve that: one is run nose with xunit plugin with
`--collect-only` and parse the resulting junit xml file. Alternatively, you
can add a basic plugin that captures names of the tests, something like this:

    
    
    import sys
    from unittest import TestCase
    
    import nose
    from nose.tools import set_trace
    
    
    class CollectPlugin(object):
        enabled = True
        name = "test-collector"
        score = 100
    
    
        def options(self, parser, env):
            pass
    
        def configure(self, options, conf):
            self.tests = []
    
        def startTest(self, test):
            self.tests.append(test)
    
    class MyTestCase(TestCase):
        def test_long_integration(self):
            pass
        def test_end_to_end_something(self):
            pass
    
    
    
    if __name__ == '__main__':
        # this code will run just this file, change module_name to something 
        # else to make it work for folder structure, etc
    
        module_name = sys.modules[__name__].__file__
    
        plugin = CollectPlugin()
    
        result = nose.run(argv=[sys.argv[0],
                                module_name,
                                '--collect-only',
                                ],
                          addplugins=[plugin],)
    
        for test in plugin.tests:
            print test.id()
    

Your test information is all captured in `plugin.test` structure.

