名称'pytest'未定义。(Name 'pytest' is not defined. how to run testpy?)

系统教程 行业动态 更新时间:2024-06-14 16:57:18
名称'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.py

My test script

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

I have imported pytest

import pytest

I 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 defined

In 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)) == expected

Navigate to the directory containing the file test_set.py and execute the pytest command:

pytest

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

更多推荐

本文发布于:2023-04-12 20:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/afd47a5d3a4082190aa683c420a51b05.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:名称   未定义   pytest   testpy   defined

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!