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

fab*_*ota 2 python pytest

我已经用 conda 安装了 python。

pytest --version
This is pytest version 3.0.5, imported from /home/fabiano/anaconda3/lib/python3.6/site-packages/pytest.py
Run Code Online (Sandbox Code Playgroud)

我的测试脚本

def tc1():
    given="49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d"
    expected=b"SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t"
    assert base64.b64encode(bytes.fromhex(given)) == expected
Run Code Online (Sandbox Code Playgroud)

我已经导入了 pytest

import pytest
Run Code Online (Sandbox Code Playgroud)

我正在用 pytest 尝试一些东西。但是当我从 Python shell 尝试时,我遇到了这样的问题

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

在我的 shell pytest 中

<module 'pytest' from '/home/fabiano/anaconda3/lib/python3.6/site-packages/pytest.py'>
Run Code Online (Sandbox Code Playgroud)

我应该在哪里保存 testset.py 文件?

Pie*_*uyl 5

pytest 进行测试发现。他们的文档中很好地列出了基本步骤

  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
Run Code Online (Sandbox Code Playgroud)

导航到包含该文件的目录test_set.py并执行 pytest 命令:

pytest
Run Code Online (Sandbox Code Playgroud)

预期输出:

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 ===========================
Run Code Online (Sandbox Code Playgroud)