2017-06-28 2 views
2

私はcondaでpythonをインストールしました。名前 'pytest'は定義されていません。 testpyを実行する方法?

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

私のテストスクリプト

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

私はPythonのシェルからしようとしたとき、私はpytest.Butでいくつかのものをしようとしています

import pytest 

、私はこの

ような問題を抱えているpytest輸入してきました
testset.py 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
NameError: name 'testset' is not defined 

私のsh私はtestset.pyファイルを保存する必要がありエル pytest

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

+1

pythonスクリプトにpytestをインポートしましたか? (私たちはあなたのテストスクリプトの例を得ることができます) –

+2

標準のシェルとして実行しているかのようにpythonシェルから 'testset.py'を実行しようとしているようです。 Pythonはすべてをbashのような文字列として扱いません。 –

+0

@KindStrangerどうすればいいですか? –

答えて

3

pytestはテストディスカバリを実行します。基本的な手順はよくテストtest_*.pyまたは*_test.pyを含むファイルの名前を指定し、そのdocumentation

  1. に記載されています。
  2. 名前、それらのファイルのテスト機能def test_*

あなたのために、テストファイルtest_set.pyに次のコードを配置:ファイルtest_set.pyを含むディレクトリに

import base64 

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

移動しpytestコマンドを実行します:

pytest 

期待される出力:

[email protected]:/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 ===========================