名称'pytest'未定义。(Name 'pytest' is not defined. how to run testpy?)
我用conda安装了python。
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我已经导入了pytest
import pytest我正在尝试pytest的一些东西。但是当我从Python shell尝试时,我遇到了这样的问题
testset.py Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'testset' is not defined在我的shell pytest中
<module 'pytest' from '/home/fabiano/anaconda3/lib/python3.6/site-packages/pytest.py'>我应该在哪里保存testset.py文件?
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.pyMy test script
def tc1(): given="49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d" expected=b"SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t" assert base64.b64encode(bytes.fromhex(given)) == expectedI have imported pytest
import pytestI 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 definedIn my shell pytest
<module 'pytest' from '/home/fabiano/anaconda3/lib/python3.6/site-packages/pytest.py'>Where should I save testset.py file?
最满意答案
pytest测试发现。 基本步骤已在其文档中列出
将包含测试test_*.py的文件命名为test_*.py *_test.py或*_test.py 。 在这些文件中命名测试函数def test_*为了测试,将以下代码放在test_set.py文件中:
import base64 def test_tc1(): given="49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d" expected=b"SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t" assert base64.b64encode(bytes.fromhex(given)) == expected导航到包含文件test_set.py的目录并执行pytest命令:
pytest预期产量:
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 ===========================pytest does test discovery. The basic steps are well listed in their documentation
Name the file containing the test test_*.py or *_test.py. 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)) == expectedNavigate to the directory containing the file test_set.py and execute the pytest command:
pytestExpected 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 ===========================更多推荐
发布评论