pytest_sessionstart/finish 不会打印

pytest_sessionstart/finish won't print

这让我抓狂。我正在关注在多个位置发布的一个基本示例,它只是没有打印到控制台,而且我不知道我还可以在界面中调整什么以使其工作。

import unittest, pytest

print("module")

def pytest_sessionstart(session):
  print("before")

@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
  outcome = yield
  result = outcome.get_result()
  if result.when == 'call':
    item.session.results[item] = result

def pytest_sessionfinish(session, exitstatus):
  print("after")

class TestFoo(unittest.TestCase):
  def test_foo(self):
    pass

然后:

#> pytest tests/test.py -s
======================================================== test session starts =========================================================
platform darwin -- Python 3.7.3, pytest-5.3.2, py-1.8.1, pluggy-0.13.1
rootdir: /Users/sean
collecting ... module
collected 1 item                                                                                                                     

tests/test.py .

========================================================= 1 passed in 0.01s ==========================================================

所以...伙计,我的输出在哪里?我打印了 "module",但没有打印 "before" 或 "after"。我确定我遗漏了一些基本的东西。

pytest_sessionstartpytest_sessionfinishinitialization hooks,需要插件和 conftest.py 文件。

将它们的定义移动到 test.py 旁边的 conftest.py 文件中。