简体   繁体   中英

pytest doesn't find the test

I try to make a simple test with assert statement like this

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

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

! pytest

and it returns

在此输入图像描述

If anyone has any ideas what might be wrong.

For pytest to find the tests these have to be in a file in the current directory or a sub-directory and the filename has to begin with test (although that's probably customizable).

So if you use (inside a Jupyter notebook):

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

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

That creates a file named test_sth.py with the given contents.

Then run ! pytest ! pytest and it should work (well, fail):

============================= 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 ===========================

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