pytest没有找到测试

时间:2018-01-06 22:19:38

标签: python pytest assert

我尝试用这样的断言语句做一个简单的测试

def sqr(x):
    return x**2 + 1 

def test_sqr():
    assert kvadrat(2) == 4

! pytest

然后返回

enter image description here

如果有人有任何想法可能会出错。

1 个答案:

答案 0 :(得分:1)

pytest找到测试,这些测试必须位于当前目录或子目录中的文件中,文件名必须以test开头(尽管这可能是可定制的。)

所以如果你使用(在Jupyter笔记本内):

%%file test_sth.py
def sqr(x):
    return x**2 + 1 

def test_sqr():
    assert kvadrat(2) == 4

创建一个名为test_sth.py的文件,其中包含给定的内容。

然后运行! pytest它应该有效(好吧,失败):

============================= test session starts =============================
platform win32 -- Python 3.6.3, pytest-3.3.0, py-1.5.2, pluggy-0.6.0
rootdir: D:\JupyterNotebooks, inifile:
plugins: hypothesis-3.38.5
collected 1 item

test_sth.py F                                                            [100%]

================================== FAILURES ===================================
__________________________________ test_sqr ___________________________________

    def test_sqr():
>       assert kvadrat(2) == 4
E       NameError: name 'kvadrat' is not defined

test_sth.py:5: NameError
========================== 1 failed in 0.12 seconds ===========================
相关问题