ModuleNotFoundError: No module named 'src.testproject'

ModuleNotFoundError: No module named 'src.testproject'

我一直在尝试使用 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) 上的位置直接复制到 tests 目录,但是 testproject 需要更多的东西,它们也在 site-packages 目录中。因此,与其复制我需要的所有内容以使其在站点包中运行,不如我需要做什么?我可以将整个路径以某种方式放入导入语句吗?

我在 conftest.py 中添加了以下代码行:

sys.path.append("C:\Python39\Lib\site-packages")

现在我可以生成人类可读的 HTML 格式的测试报告。完整代码如下:

import pytest
import json
import sys

sys.path.append("C:\Python39\Lib\site-packages")
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',
              report_path="C:\Users\Trevor\source\repos\ASP.NET project_2\WebUI_testing\reports")
  elif config_browser == 'firefox':
    driver = webdriver.Firefox(
              token='6oBP2BZTPq9zluYpix_3sbwJzP4w005KZOn5bsrMzF01',
              report_path="C:\Users\Trevor\source\repos\ASP.NET project_2\WebUI_testing\reports")
  else:
    raise Exception(f'"{config["browser"]}" is not supported')

  driver.implicitly_wait(config_wait_time)
  yield driver
  driver.quit()