2017-06-05 11 views
3

私がpytestを使用しているときのほとんどは、出力が非常に長くなります。百線以上。そして、しばしば私はこのアウトプットを欲しがる時代です。 --tb=shortではなく、実際にはという非常に良いアプローチです。しかし、私は自分のテスト結果を見つけるためにtmuxウィンドウの200行をスクロールしたくないので、それはだから超厄介です。私が持っているのが大好きだ何pytestを使ってテスト*の後にテスト名*を表示するにはどうしたらいいですか?

は次のようなものです:

______________________ >>test_my_test_with_a_lot_of_output _______________________ 
# imagine lots of test output here 
______________________ <<test_my_test_with_a_lot_of_output _______________________ 

は、私がこのような出力を達成するためにpy.testに使用できるいくつかのフラグや設定はありますか?

答えて

0

フックを使用してこれを実現する簡単な方法が見つかりませんでした。しかし、ここでこれを実装する方法があります。 これは理想的な実装ではありません。

# contest.py 
import pytest 
import _pytest 

class TerminalReporter(_pytest.terminal.TerminalReporter): 
    def _gettestname(self, rep): 
     # actually "rename" original method for clarity 
     return super()._getfailureheadline(rep) 

    def _getfailureheadline(self, rep): 
     # instead of test name 
     # (which is originally printed at the top of report) 
     # return prefixed name 
     return '>>' + self._gettestname(rep) 

    def _outrep_summary(self, rep): 
     super()._outrep_summary(rep) 
     # after printing test report end, print out test name again 
     # XXX: here we hard-code color, so red will be used even for passed tests 
     # (if passes logging is enabled) 
     # You can add some more logic with analyzing report status 
     self.write_sep('_', '<<' + self._gettestname(rep), red=True) 

@pytest.hookimpl(trylast=True) 
def pytest_configure(config): 
    # overwrite original TerminalReporter plugin with our subclass 
    # we want this hook to be executed after all other implementations 
    # to be able to unregister original plugin 
    reporter = TerminalReporter(config) 
    config.pluginmanager.unregister(name='terminalreporter') 
    config.pluginmanager.register(reporter, 'terminalreporter') 

このアプローチを拡張することのインスピレーションのためには、_pytest.terminal.TerinalReporterクラスソースを参照してください。

1

メイン/ルートconftest.pyに1つのフィクスチャを追加できます。これは、テストケースの前後に自動的に呼び出されます。同様

@pytest.fixture(scope='function', autouse=True) 
def test_log(request): 
    logging.info("Test '{}' STARTED".format(request.node.nodeid)) # Here logging is used, you can use whatever you want to use for logs 
    def fin(): 
     logging.info("Test '{}' COMPLETED".format(request.node.nodeid)) 
    request.addfinalizer(fin) 

ここでは、request.node.nodeidはあなたにテストの名前を与えます。

+0

ニースアプローチ。ただ一つの小さな問題は、テストセットアップ節に 'STARTED'が、テストティアダウン節に' COMPLETED'が出力されることです。 '[captured stdout setup] Test abc STARTED; [キャプチャされたstdout呼び出し]ここで長い出力... [キャプチャされたstdoutティアダウン]テストabc COMPLETED'しかし、このアプローチはまだ私よりもエレガントです。 – MarSoft

関連する問題