【报错:No module named pytest】

No module named pytest

问题:

写了一个pytest的配置文件,想在命令行通过pytest启动配置文件来运行测试用例。报错:No module named pytest

排查:

pytest我肯定是安装过了,但是为什么还会找不到该模块。我打开pycharm的Python Interpreter查看确实是有的
在这里插入图片描述
于是在终端输入:

which python3

在这里插入图片描述

原因

系统默认解释器和pycharm选择的解释器不是同一个,系统默认的那个python解释器里没有安装pytest
在这里插入图片描述

解决

系统默认python3解释器是在/usr/bin/python3下,我回到pycharm——Preferences——Python Interpreter——右上角设置——+号——Vitualenv Environment——New Environment——Base Interpreter选择在该/usr/bin/python3目录下的python解释器。
在这里插入图片描述
发现果然没有pytest
在这里插入图片描述
点击左上角+号,搜索pytest点击install可成功安装。Terminal中输入:
pytest可成功运行
在这里插入图片描述
test_add_01.py:

def add(x, y):
    return x + y


class TestAdd:

    def setup(self):
        print("测试用例开始执行")

    def test_add_01(self):
        result = add(1, 2)
        assert result == 3

    def test_add_02(self):
        result = add(2, 2)
        assert result == 5

    def teardown(self):
        print("测试用例执行结束")
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当出现"ModuleNotFoundError: No module named 'pytest'"的错误时,表示你的Python环境中没有安装pytest模块。你可以按照以下方法解决这个问题: 解决方法1:使用PyCharm配置unittest运行pytest 1. 在PyCharm中,选择菜单栏中的"Run"->"Edit Configurations..."。 2. 在打开的窗口中,选择"Python tests"->"Unittests"。 3. 在"Target"中选择需要运行的unittest文件,并点击"OK"保存配置。 4. 之后,你可以在菜单栏中选择"Run"->"Run 'Unittests'"来运行unittest。 解决方法2:更改PyCharm的默认测试运行工具为unittest 1. 打开PyCharm,选择菜单栏中的"File"->"Settings"。 2. 在弹出的Settings窗口中,选择"Tools"->"Python Integrated Tools"。 3. 在右侧的"Default test runner"下拉框中选择"unittest",然后点击"Apply"和"OK"保存更改。 4. 之后,你可以在菜单栏中选择"Run"->"Run 'Unittests'"来运行unittest。(如果之前已经生成了pytest配置,在"Run"->"Edit Configurations..."中点击减号删除) 解决方法3:安装pytest模块 1. 打开PyCharm,并在菜单栏中选择"File"->"Settings"。 2. 在弹出的Settings窗口中,选择"Project"->"Python Interpreter"。 3. 右侧列表中找到pytest,点击"+"按钮进行安装。 4. 安装完成后,你应该可以正常使用pytest模块来运行你的测试。 以上是解决"ModuleNotFoundError: No module named 'pytest'"错误的几种方法,你可以根据自己的情况选择适合的方法进行尝试。希望对你有帮助!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [自动化测试python 3.6+pycharm 2018+selenium +unittest和pytest ](https://download.csdn.net/download/XG_17_38241417116/85236217)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [Pycharm运行unittest报错ModuleNotFoundError: No module namedpytest‘解决](https://blog.csdn.net/Zhou_ZiZi/article/details/130591116)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [pytest报错ModuleNotFoundError: No module namedpytest](https://blog.csdn.net/WXY111MAO/article/details/131583218)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值