fabiano.mota
fabiano.mota

Reputation: 307

Name 'pytest' is not defined. 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 have imported pytest

import pytest

I am trying some stuff with pytest.But when I try from 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 shell pytest

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

Where should I save testset.py file?

Upvotes: 3

Views: 15225

Answers (1)

Pierre de Buyl
Pierre de Buyl

Reputation: 7293

pytest does test discovery. The basic steps are well listed in their documentation

  1. Name the file containing the test test_*.py or *_test.py.
  2. Name the test functions in those files def test_*

For you test, place the following code in the file test_set.py:

import base64

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

Navigate to the directory containing the file test_set.py and execute the pytest command:

pytest

Expected output:

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

Upvotes: 5

Related Questions