【发布时间】:2025-09-13 04:25:02
【问题描述】:
我一直在尝试使用 TestProject OpenSDK for Python 为我的自动化测试(使用 pytest)生成 HTML 测试报告,但我收到以下错误:没有名为“src.testproject”的模块。
我已按照项目 GitHub 上的说明进行操作:https://github.com/testproject-io/python-opensdk,但我不确定问题出在哪里。
我所有的装置都在一个名为 conftest.py 的文件中。代码如下。
import pytest
import json
from src.testproject.sdk.drivers import webdriver
CONFIG_PATH = 'tests/config.json'
DEFAULT_WAIT_TIME = 10
SUPPORTED_BROWSERS = ['chrome','firefox']
@pytest.fixture(scope='session')
def config():
with open(CONFIG_PATH) as config_file:
data = json.load(config_file)
return data
@pytest.fixture(scope='session')
def config_browser(config):
if 'browser' not in config:
raise Exception('The config file does not contain "browser"')
elif config['browser'] not in SUPPORTED_BROWSERS:
raise Exception(f'"{config["browser"]}" is not a supported browser')
return config['browser']
@pytest.fixture(scope='session')
def config_wait_time(config):
return config['wait_time'] if 'wait_time' in config else DEFAULT_WAIT_TIME
@pytest.fixture
def browser(config_browser, config_wait_time):
if config_browser == 'chrome':
driver = webdriver.Chrome(token='6oBP2BZTPq9zluYpix_3sbwJzP4w005KZOn5bsrMzF01')
elif config_browser == 'firefox':
driver = webdriver.Firefox(token='6oBP2BZTPq9zluYpix_3sbwJzP4w005KZOn5bsrMzF01')
else:
raise Exception(f'"{config["browser"]}" is not supported')
driver.implicitly_wait(config_wait_time)
yield driver
driver.quit()
顶部的导入语句与说明一致,我对“浏览器”夹具(文件中的最后一个夹具)进行了必要的更改,以将开发人员令牌作为参数传递给驱动程序构造函数.
conftest.py 文件和 JSON 配置文件都与测试驱动程序一起位于 tests 目录中,但我从下一个最高目录运行测试:WebUI_testing,所以我不确定它为什么抱怨。
编辑 1 我尝试将 src 目录(其中包含 testproject)从我的 C: 驱动器 (C:\Python39\Lib\site-packages) 上的位置直接复制到测试目录,但是 testproject 需要更多的东西,这些东西也在站点中-包目录。因此,与其复制我需要的一切以使其从站点包中运行,我需要做什么?我可以以某种方式将整个路径放入 import 语句中吗?
【问题讨论】:
-
我在这里没有看到名为
src或testproject的文件夹。 -
testproject 在我的 Python 文件夹中:C:\Python39\Lib\site-packages。我需要将 src 文件夹移动到我的测试项目中吗?还是我需要更改导入语句?
-
你需要从包含 src 的路径开始代码。以上面的目录为例。
-
我的测试项目和src不在同一个目录。当我使用 pip 安装 testproject 时,它把它放在 Python 目录中。
-
什么是
pipenv run python -m site?
标签: python automated-tests pytest test-project