1

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

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).

Community
  • 1
  • 1

1 Answers1

0

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.

Oleksiy
  • 5,813
  • 3
  • 33
  • 52
  • With my setup, --xunit --collect-only creates a notetests.xml file with exactly the information I needed. Thank you so much! – Matt Bliese Apr 01 '15 at 13:40