nose-devs / nose

nose is nicer testing for python
http://readthedocs.org/docs/nose/en/latest/
1.36k stars 395 forks source link

Is there an hook to change the default header output by "nose"? #1072

Open aaronlelevier opened 6 years ago

aaronlelevier commented 6 years ago

I'd like to be able to change the default header output on a failed test. I'd like to change it from:

======================================================================
FAIL: test_create_currency (accounting.tests.test_factory.FactoryTests)
----------------------------------------------------------------------

To:

======================================================================
FAIL: (accounting.tests.test_factory:FactoryTests.test_create_currency)
----------------------------------------------------------------------

The reason that I want to do this is that I frequently copy and paste the header text in order to re-run a single failed test. But, the standard output doesn't let you easily copy and paste it into the terminal, so I'd like to be able to update the output to do format it within the "(...)" so it is the library's accepted format for running a single test.

In order to do this override, I would like to do use the following code, but I am not sure where to inject it.


class TextTestResult(NoseTextTestResult):

    def getDescription(self, test):
        """
        Overrides the default test header, so the failed test name
        can be copied as is and syntax works to be able to run the
        single test "as copied" from this output.

        Example:
            'myapp.tests.test_models:MyModelTests.test_foo'

        Returns:
            str (always)
        """
        return '({module}:{test_class}.{test_method})'.format(
            module=test.test.__class__.__module__,
            test_class=test.test.__class__.__name__,
            test_method=test.test._testMethodName)

Can you advise if this is possible? Or how to inject / override to get this behavior?

Thanks