【报错:No module named pytest】

No module named pytest

问题:

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

排查:

pytest我肯定是安装过了,但是为什么还会找不到该模块。我打开pycharm的Python Interpreter查看确实是有的
【报错:No module named pytest】_第1张图片
于是在终端输入:

which python3

【报错:No module named pytest】_第2张图片

原因

系统默认解释器和pycharm选择的解释器不是同一个,系统默认的那个python解释器里没有安装pytest
【报错:No module named pytest】_第3张图片

解决

系统默认python3解释器是在/usr/bin/python3下,我回到pycharm——Preferences——Python Interpreter——右上角设置——+号——Vitualenv Environment——New Environment——Base Interpreter选择在该/usr/bin/python3目录下的python解释器。
【报错:No module named pytest】_第4张图片
发现果然没有pytest
【报错:No module named pytest】_第5张图片
点击左上角+号,搜索pytest点击install可成功安装。Terminal中输入:
pytest可成功运行
【报错:No module named pytest】_第6张图片
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("测试用例执行结束")

你可能感兴趣的:(pytest,python,开发语言)