0

I'm looking for a way to customize the output of python unittests. So far, I think my best chance is using pytest hooks to achieve my goal.

Regardless, I've tried to implement something myself and have been fairly successful in navigating test classes with Python's AST. I've used it to find all test classes, functions and nodes executing "assertEqual" or similar expressions for testing.

Now the next step would be to load the tests dynamically, execute them, redirect the output to a file and then parse this file to obtain the actual test results, so I can combine both the information from the AST and from the actual execution. But I'm really stuck here, I've tried several methods, but I'm unable to load the testcase in a TestSuite, which I would then pass to a TextTestRunner, which would write the output to a file.

I've tried the following code:

"~/dev/python/myproject/tests/mytestclass.py": full path to the test class I want to load "~/dev/python/custom_tests/custom_tests/parser.py": full path to the file that shall load the test class given above

  1. I add the path to the project ("~/dev/python/myproject/") to my path

    sys.path.insert(0, self.path)
    
  2. I try to find the module "tests"

    f, pathname, desc = imp.find_module("tests")
    module = imp.load_module("tests", f, pathname, desc)
    
  3. I try to load mytestclass

    f, pathname, desc = imp.find_module("test_status_code", module.__path__)
    test = imp.load_module("mytestclass", f, pathname, desc)
    

This works so far, but how can I add this to a TestSuite?

m-strasser
  • 78
  • 6
  • Isn't [unittest-xml-reporting](https://github.com/xmlrunner/unittest-xml-reporting) a better way to receive a fully customizable output? I mean you can apply any xslt-based postprocessing on it. – user3159253 Apr 23 '16 at 08:27
  • You might be abusing pytests to do something it is not designed for. `pytest` (and test frameworks in general) are meant to define test cases, run them and report results. You seem to try generating some data or report. For this purpose, consider other tools like `doit`, which can define number of tasks to run and the tasks can generate anything you can do with python. – Jan Vlcinsky Apr 23 '16 at 08:36
  • didn't know about that, thanks :) – m-strasser Apr 23 '16 at 08:37

0 Answers0