Seward Hercules
Seward Hercules

Reputation: 107

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

enter image description here

If anyone has any ideas what might be wrong.

Upvotes: 7

Views: 7690

Answers (1)

MSeifert
MSeifert

Reputation: 152820

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

Upvotes: 2

Related Questions