The name "pytest" is undefined. how to run testpy?

I have installed python with conda.

pytest --version
This is pytest version 3.0.5, imported from /home/fabiano/anaconda3/lib/python3.6/site-packages/pytest.py

      

My test script

def tc1():
    given="49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d"
    expected=b"SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t"
    assert base64.b64encode(bytes.fromhex(given)) == expected

      

I imported pytest

import pytest

      

I am trying to use some stuff with pytest. But when I try to use Python shell I have problems like this

testset.py
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'testset' is not defined

      

In my pytest shell

<module 'pytest' from '/home/fabiano/anaconda3/lib/python3.6/site-packages/pytest.py'>

      

Where to save the testet.py file?

+3


source to share


1 answer


pytest does the check. The main steps are well listed in the documentation

  • Name the file containing the test test_*.py

    or *_test.py

    .
  • Name the test functions in these files def test_*

For a test, put the following code in a file test_set.py

:

import base64

def test_tc1():
    given="49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d"
    expected=b"SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t"
    assert base64.b64encode(bytes.fromhex(given)) == expected

      



Change to the directory containing the file test_set.py

and run the pytest command:

pytest

      

Expected Result:

user@pc:/tmp/test_dir $ pytest .
============================= test session starts ==============================
platform linux -- Python 3.5.2+, pytest-3.0.3, py-1.4.31, pluggy-0.4.0
rootdir: /tmp/test_dir , inifile:  collected 1 items 

test_set.py .

=========================== 1 passed in 0.01 seconds ===========================

      

+4


source







All Articles