简体   繁体   中英

Change the name of py.test tests

I'm interested in migrating my test suite from using nose to using py.test. I don't like the test names printed by py.test in the test failure summary, because they don't include the file name of the test. I do really like the test names that py.test uses for to print progress in verbose mode.

For example:

[dbw tests]$ py.test -sv r_group/test_meta_analysis/test_meta_analysis.py::_TestPanel::testDisplaySummary
==================================== test session starts ===============================
collected 3 items

r_group/test_meta_analysis/test_meta_analysis.py::_TestPanel::testDisplaySummary FAILED

========================================= FAILURES =====================================
_______________________________ _TestPanel.testDisplaySummary __________________________

This is pretty important to me because I have ~40K tests, and the short name "_TestPanel.testDisplaySummary" is not helpful to me in quickly finding the test I want. I assume that there is a built-in py.test hook that will do this, but I haven't found it yet.

The method summary_failures() of _pytest.terminal.TerminalReporter is the one that prints that line (I found it by searching for the string "FAILURES"). It uses _getfailureheadline() to get the test name (and to discard the file name and line number).

I'd suggest subclassing TerminalReporter and override _getfailureheadline() .

For example:

def _getfailureheadline(self, rep):
    if hasattr(rep, 'location'):
        fspath, lineno, domain = rep.location
        return '::'.join((fspath, domain))
    else:
        return super()._getfailureheadline(rep)

Produces the following output:

test.py::test_x FAILED

================ FAILURES ================
____________ test.py::test_x _____________

You can override the default reporter with your own by writing a new plugin with the following:

class MyOwnReporter(TerminalReporter):
    ...

def pytest_configure(config):
    reporter = MyOwnReporter(config, sys.stdout)
    config.pluginmanager.register(reporter, 'terminalreporter')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM