How to selet a specific Test with python unitest testsuites
Question: I have python unittest code organized as follows:

    
    
    Maindir
       |
       |--Dir1
       |  |
       |  |-- test_A.py
       |  |-- test_B.py
       |  |-- test_C.py
       |
       |--Dir2
           | ...
    

I assume you get the picture. In each of the `Dirx` directories I have a file
named `suite.py` which puts together a suite of tests from the tests in the
give directory (so you can choose specific test, omit other tests etc.). These
files look e.g. like the following (in case to choose all tests, they might
also select only a subset of tests) [also consider test <-> unittest]:

    
    
    import test_A
    import test_B
    import test_C
    
    suite1 = test.TestSuite()
    suite1.addTests(test.TestLoader().loadTestsFromTestCase(test_A.MyTest))
    suite1.addTests(test.TestLoader().loadTestsFromTestCase(test_B.MyTest))
    suite1.addTests(test.TestLoader().loadTestsFromTestCase(test_C.MyTest))
    

The main Runner `execall.py` in the `Maindir` directory looks like this

    
    
    from Dir1.suite import suite1
    from Dir2.suite import suite2
    
    suite_all = test.TestSuite([
        suite1,
        suite2])
    
    if __name__ == '__main__':
        test.main(defaultTest='suite_all')
    

Now I can do the following:

  * Run all tests: 'execall.py' (as documented)
  * Run a specific suite: `execall.py suite1` (as documented)

But how to run only a specific single test? And how to run all tests of the
specific file? I tried the following without success, with the same error:
`'TestSuite' object has no attribute 'xxx'`

    
    
    execall.py suite1.test_A
    execall.py suite1.test_A.test1
    execall.py test_A
    execall.py test_A.test1
    

`execall.py -h` gives very specific Examples how to run single tests or tests
in testcases, but in my case this does not seem to work. Any ideas are
appreciated.


Answer: One way to do it would be writing your own tests loader. I highly recommend
adopting the mechanism found in [Flask's testsuite
module](https://github.com/mitsuhiko/flask/blob/master/flask/testsuite/__init__.py).

The basic idea is:

  1. Implement a routine which returns a `unittest.TestSuite()` object with all the Python modules which contain the required tests. This could be done e.g. by scanning the directory for `test_XXX.py` files (simply checking them by `startswith('test')` or regexp etc.).

  2. Subclass `unittest.TestLoader` and override `loadTestsFromName(self, name, module)` which would use the testsuite generated in Step1. For example, in Flask:
    
        for testcase, testname in find_all_tests(suite):
        if testname == name or \
            testname.endswith('.' + name) or \
            ('.' + name + '.') in testname or \
            testname.startswith(name + '.'):
            all_tests.append(testcase)
    

This allows loading tests by python module name, by test suite (test class)
name or just by test case name.

