Mefine 发表于 2023-7-19 10:48:14

playwright+pytest执行用例报错No module named 'pytest_base_url'

直接上源码
import pytest
from py.xml import html
from config import RunConfig
import allure
from slugify import slugify
from common.config import *
from playwright.sync_api import sync_playwright, expect



# 定义基本测试环境
@pytest.fixture(scope='session')
def base_url():
    return RunConfig.baseUrl

import os
from common.config import *

'''
运行测试配置
'''

class RunConfig:

    # 运行测试用例的目录或文件
    # case_path = os.path.join(case_path, "test_case")
    cases_path = os.path.join(case_path, "test_demo.py")
    #

    # 配置浏览器驱动类型(chromium, firefox, webkit)
    browser = "chromium"

    # 运行模式(headless, headful)
    # mode = "headless"
    mode = "headful"

    # 配置运行的 URL
    baseUrl = "XXX"

    # 失败重跑次数
    rerun = "0"

    # 当达到最大失败数,停止执行
    max_fail = "5"

    # 报告路径(不需要修改)
    NEW_REPORT = None

import sys
from time import sleep
from playwright.async_api import Dialog
from seldom.testdata.conversion import json_to_list
from page.element import Element
import allure
import pytest
from common.config import*

@allure.feature('平台基础测试')
class TestPlatformFoundation():

    @allure.story("test001-平台登录")
    def test_platform_foundation_001(self, page, base_url):
      """
      名称:平台登录
      步骤:
      1、打开浏览器
      2、输入账号
      3、输入密码
      4、点击登录
      检查点:
      * 检查登录是否成功
      """
      page.goto(base_url)

报错如下:
Traceback (most recent call last):
File "D:\Program Files\JetBrains\PyCharm 2021.3.2\plugins\python\helpers\pycharm\_jb_pytest_runner.py", line 51, in <module>
    sys.exit(pytest.main(args, plugins_to_load + ))
File "D:\python\lib\site-packages\_pytest\config\__init__.py", line 147, in main
    config = _prepareconfig(args, plugins)
File "D:\python\lib\site-packages\_pytest\config\__init__.py", line 328, in _prepareconfig
    config = pluginmanager.hook.pytest_cmdline_parse(
File "D:\python\lib\site-packages\pluggy\hooks.py", line 286, in __call__
    return self._hookexec(self, self.get_hookimpls(), kwargs)
File "D:\python\lib\site-packages\pluggy\manager.py", line 93, in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
File "D:\python\lib\site-packages\pluggy\manager.py", line 84, in <lambda>
    self._inner_hookexec = lambda hook, methods, kwargs: hook.multicall(
File "D:\python\lib\site-packages\pluggy\callers.py", line 203, in _multicall
    gen.send(outcome)
File "D:\python\lib\site-packages\_pytest\helpconfig.py", line 103, in pytest_cmdline_parse
    config: Config = outcome.get_result()
File "D:\python\lib\site-packages\pluggy\callers.py", line 80, in get_result
    raise ex.with_traceback(ex)
File "D:\python\lib\site-packages\pluggy\callers.py", line 187, in _multicall
    res = hook_impl.function(*args)
File "D:\python\lib\site-packages\_pytest\config\__init__.py", line 1075, in pytest_cmdline_parse
    self.parse(args)
File "D:\python\lib\site-packages\_pytest\config\__init__.py", line 1425, in parse
    self._preparse(args, addopts=addopts)
File "D:\python\lib\site-packages\_pytest\config\__init__.py", line 1305, in _preparse
    self.pluginmanager.load_setuptools_entrypoints("pytest11")
File "D:\python\lib\site-packages\pluggy\manager.py", line 299, in load_setuptools_entrypoints
    plugin = ep.load()
File "D:\python\lib\importlib\metadata.py", line 86, in load
    module = import_module(match.group('module'))
File "D:\python\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name, package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 972, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 984, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'pytest_base_url'

进程已结束,退出代码1

空套件

陶远航 发表于 2023-7-19 10:48:39

根据您提供的代码和报错信息,可以看出问题是找不到名为 'pytest_base_url' 的模块。根据代码分析,没有在代码中找到导入 'pytest_base_url' 模块的语句。因此,系统在运行时无法找到该模块,从而抛出了 ModuleNotFoundError 错误。

解决这个问题的方法是确保 'pytest_base_url' 模块已经正确安装,并在代码的合适位置进行导入。如果该模块是您自己编写的,您需要将模块文件(.py 文件)放置在正确的位置,并使用正确的导入语句将其引入到代码中。如果是第三方模块,您可以使用以下命令安装该模块:

pip install pytest_base_url


请注意,我这里假设 'pytest_base_url' 是一个真正存在的模块名。如果是其他模块名,请使用正确的模块名进行安装和导入。

另外,还需要检查代码中是否存在其他导入语句,可能存在其他未安装或未导入的模块,需要一并进行安装和导入操作。

希望我的回答对您有帮助!如果还有其他问题,请随时提问。

isdkz 发表于 2023-7-19 10:48:46

错误提示显示你的环境中没有找到名为 'pytest_base_url' 的模块。你需要安装这个模块才能解决这个问题。

你可以使用以下命令来安装 'pytest_base_url':

pip install pytest-base-url


这个命令会从Python的包索引(PyPI)下载并安装 'pytest-base_url'。

如果你已经安装了这个模块,但仍然收到这个错误,那可能是因为你的Python环境没有正确设置。你需要确保你的Python解释器和运行你的代码的解释器是同一个。你可以检查你的IDE设置(例如PyCharm)或你的环境变量来确认这一点。

如果你使用的是虚拟环境,你需要在虚拟环境中安装 'pytest-base_url'。你可以先激活你的虚拟环境,然后再运行上面的安装命令。

如果以上步骤都不能解决你的问题,你可能需要检查你的代码是否有错误。例如,你可能在不应该导入 'pytest_base_url' 的地方导入了它,或者你可能错误地导入了一个名字类似但实际上不存在的模块。

Mefine 发表于 2023-7-19 10:50:33

isdkz 发表于 2023-7-19 10:48
错误提示显示你的环境中没有找到名为 'pytest_base_url' 的模块。你需要安装这个模块才能解决这个问题。

...

前面两步都做了,还是报错,并不是虚拟环境,那么我需要创建虚拟环境安装pytest_base_url吗

Mefine 发表于 2023-7-19 11:13:16

陶远航 发表于 2023-7-19 10:48
根据您提供的代码和报错信息,可以看出问题是找不到名为 'pytest_base_url' 的模块。根据代码分析,没有在 ...

pip安装过了还是报错
(<class 'ModuleNotFoundError'>, ModuleNotFoundError("No module named 'pytest_base_url'"), <traceback object at 0x0000020E294ABD40>)

Mefine 发表于 2023-7-19 11:14:54

PS D:\Project\defineuitest> pip install pytest_base_url
Requirement already satisfied: pytest_base_url in d:\python\lib\site-packages (2.0.0)
Requirement already satisfied: pytest<8.0.0,>=3.0.0 in d:\python\lib\site-packages (from pytest_base_url) (7.4.0)
Requirement already satisfied: requests>=2.9 in d:\python\lib\site-packages (from pytest_base_url) (2.25.0)
Requirement already satisfied: colorama in d:\python\lib\site-packages (from pytest<8.0.0,>=3.0.0->pytest_base_url) (0.4.4)
Requirement already satisfied: tomli>=1.0.0 in d:\python\lib\site-packages (from pytest<8.0.0,>=3.0.0->pytest_base_url) (2.0.1)
Requirement already satisfied: exceptiongroup>=1.0.0rc8 in d:\python\lib\site-packages (from pytest<8.0.0,>=3.0.0->pytest_base_url) (1.1.2)
Requirement already satisfied: packaging in d:\python\lib\site-packages (from pytest<8.0.0,>=3.0.0->pytest_base_url) (20.8)
Requirement already satisfied: pluggy<2.0,>=0.12 in d:\python\lib\site-packages (from pytest<8.0.0,>=3.0.0->pytest_base_url) (0.13.1)
Requirement already satisfied: iniconfig in d:\python\lib\site-packages (from pytest<8.0.0,>=3.0.0->pytest_base_url) (1.1.1)
Requirement already satisfied: chardet<4,>=3.0.2 in d:\python\lib\site-packages (from requests>=2.9->pytest_base_url) (3.0.4)
Requirement already satisfied: idna<3,>=2.5 in d:\python\lib\site-packages (from requests>=2.9->pytest_base_url) (2.10)
Requirement already satisfied: certifi>=2017.4.17 in d:\python\lib\site-packages (from requests>=2.9->pytest_base_url) (2023.5.7)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in d:\python\lib\site-packages (from requests>=2.9->pytest_base_url) (1.26.2)
Requirement already satisfied: pyparsing>=2.0.2 in d:\python\lib\site-packages (from packaging->pytest<8.0.0,>=3.0.0->pytest_base_url) (2.4.7)
页: [1]
查看完整版本: playwright+pytest执行用例报错No module named 'pytest_base_url'