1

我一直在尝试使用 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 配置文件都与测试驱动程序一起位于测试目录中,但我从下一个最高目录运行测试:WebUI_testing,所以我不确定它为什么会抱怨。

目录

编辑 1 我尝试将 src 目录(包含 testproject)从它在我的 C: 驱动器 (C:\Python39\Lib\site-packages) 上的位置直接复制到测试目录,但是 testproject 需要更多的东西,它们也在站点包目录。因此,与其复制我需要的一切以使其从站点包中运行,我需要做什么?我可以以某种方式将整个路径放入导入语句中吗?

4

1 回答 1

0

我将以下代码行添加到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()
于 2021-11-05T00:53:52.307 回答