Pytest 未检测到 FizzBuzzTest.py

Pytest not detecting FizzBuzzTest.py

这是我在 FizzBuzzTest.py

中的代码
import pytest
# content of test_sample.py
def fizzBuzz(value):
    return value

def test_returns1With1PassedIn():
    assert fizzBuzz(1) == 1

当我在命令行运行pytest -v

================================= 1 passed in 0.05 seconds ===================================================================

PS C:\Users\pytest\FizzBuzz_Kata> pytest -v

======================================== test session starts ================================================================================================

platform win32 -- Python 3.5.2, pytest-4.4.1, py-1.8.0, pluggy-0.11.0 -- c:\users\a606143\appdata\local\programs\python\python35\python.exe

cachedir: .pytest_cache

rootdir: C:\Users\pytest\FizzBuzz_Kata

collected 0 items

有人可以解释为什么 pytest 无法检测到此文件和 运行 测试吗?

我摸索了一下,似乎找到了一个解决方案,虽然有点奇怪,但很有效。

要让 pytest 检测文件,需要在文件名前面加上 test_ 命名,因此您的文件应命名为:test_FizzBussTest.py 并且pytest必须执行的函数需要具有完全相同的名称,因此您的函数需要这样命名:

def test_FizzBussTest():

编辑:经过进一步研究,该函数不需要完全按照文件命名,只需要在函数名前面加上 test_ 即可 fx test_sum():