Python 名称';pytest';没有定义。如何运行testpy?

Python 名称';pytest';没有定义。如何运行testpy?,python,pytest,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 我的测试脚本 def tc1(): given="49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573

我已经用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尝试一些东西,但是当我从pythonshell尝试时,我遇到了这样的问题

testset.py
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'testset' is not defined
testset.py
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
NameError:未定义名称“testset”
在我的壳里 皮特斯特



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

pytest执行测试发现。基本步骤在它们的列表中列出得很好

  • 将包含测试的文件命名为
    test.*.py
    *\u test.py
  • 命名这些文件中的测试函数
    def test.*
  • 要进行测试,请将以下代码放入文件
    test\u set.py

    import base64
    
    def test_tc1():
        given="49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d"
        expected=b"SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t"
        assert base64.b64encode(bytes.fromhex(given)) == expected
    
    导航到包含文件
    test\u 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 ===========================
    

    您是否在python脚本中导入了pytest?(我们可以举一个测试脚本的例子)看起来您正试图从python shell执行
    testset.py
    ,就像您作为标准shell运行一样。Python不会像bash那样将所有内容都视为字符串。我该怎么办?
    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 ===========================