未定义名称“pytest”。如何运行testpy?

2024-06-17 15:06:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经在conda中安装了python。在

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

我的测试脚本

^{pr2}$

我已经导入了pytest

import pytest

我在试一些东西pytest。但是当我尝试使用pythonshell时,我会遇到这样的问题

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

在我的壳里 pytest公司

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

我应该在哪里存钱测试集.py文件?在


Tags: frompyhomepytestisversionlibpackages
1条回答
网友
1楼 · 发布于 2024-06-17 15:06:52

pytest执行测试发现。基本步骤列在它们的documentation

  1. 将包含测试的文件命名为test_*.py*_test.py。在
  2. 将这些文件中的测试函数命名为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命令:

^{pr2}$

预期产量:

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

相关问题 更多 >