pythonautomated-testspytesttest-project

ModuleNotFoundError: No module named 'src.testproject'


I've been trying to use TestProject OpenSDK for Python to generate HTML test reports for my automated tests (with pytest), but I'm getting the following error: No module named 'src.testproject'.

Module not found

I've followed the directions laid out on the project's GitHub: https://github.com/testproject-io/python-opensdk but I'm not sure what the problem is.

All my fixtures are in a single file called conftest.py. The code is below.

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()

The import statement at the top is consistent with the instructions, and I made the necessary changes to the "browser" fixture (the last fixture in the file) to the effect of passing a developer token as an argument to the driver constructor.

The conftest.py file and the JSON config file are both in the tests directory along with the test drivers, but I run the tests from the next highest directory: WebUI_testing, so I'm not sure why it's complaining.

directories

Edit 1 I tried copying the src directory (which contains testproject) from it's location on my C: drive (C:\Python39\Lib\site-packages) directly to the tests directory, but testproject needs a lot more stuff that's also in the site-packages directory. So instead of copying everything I need to make it work out of site-packages, what do I need to do? Can I put the whole path into the import statement somehow?


Solution

  • I added the following line of code to conftest.py:

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

    And now I can produce test reports in human-readable HTML format. Complete code as follows:

    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()