From f88cff271a1ab60527a36cbfe22d8c70f3156c31 Mon Sep 17 00:00:00 2001 From: Tamer Ahmed Date: Thu, 7 May 2020 10:18:13 -0700 Subject: [PATCH] [pytest] Fix module import issue when running whole test suite (#1642) * [pytest] Fix module import issue when running whole test suite When having multiple conftest, dir leading to this conftest has to be Python package (presence of __init__.py.) Also, adding pytest basedir to conftest.py signed-off-by: Tamer Ahmed --- tests/__init__.py | 0 tests/arp/conftest.py | 2 +- tests/common/plugins/sanity_check/__init__.py | 8 -------- tests/conftest.py | 16 +++++++++++++++- tests/platform/__init__.py | 0 tests/platform/conftest.py | 2 +- tests/platform/mellanox/__init__.py | 0 .../mellanox_thermal_control_test_helper.py | 2 +- tests/platform/mellanox/test_thermal_control.py | 2 +- tests/testbed_setup/__init__.py | 0 tests/testbed_setup/conftest.py | 2 +- 11 files changed, 20 insertions(+), 14 deletions(-) create mode 100644 tests/__init__.py create mode 100644 tests/platform/__init__.py create mode 100644 tests/platform/mellanox/__init__.py create mode 100644 tests/testbed_setup/__init__.py diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/arp/conftest.py b/tests/arp/conftest.py index 734ddd75c80..71ea65eeaa4 100644 --- a/tests/arp/conftest.py +++ b/tests/arp/conftest.py @@ -1,4 +1,4 @@ -from args.wr_arp_args import add_wr_arp_args +from .args.wr_arp_args import add_wr_arp_args # WR-ARP pytest arguments def pytest_addoption(parser): diff --git a/tests/common/plugins/sanity_check/__init__.py b/tests/common/plugins/sanity_check/__init__.py index 33354715fa5..c290416f927 100644 --- a/tests/common/plugins/sanity_check/__init__.py +++ b/tests/common/plugins/sanity_check/__init__.py @@ -13,14 +13,6 @@ logger = logging.getLogger(__name__) -def pytest_addoption(parser): - """Describe plugin specified options""" - parser.addoption("--skip_sanity", action="store_true", default=False, - help="Skip sanity check") - parser.addoption("--allow_recover", action="store_true", default=False, - help="Allow recovery attempt in sanity check in case of failure") - - def _update_check_items(old_items, new_items, supported_items): """ @summary: Update the items to be performed in sanity check diff --git a/tests/conftest.py b/tests/conftest.py index eb58b40e386..827232da9f1 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,3 +1,9 @@ +# Adding pytest base dir to Python system path. +# This is required in order to import from common package including pytest_plugins within this file. +import site +from os.path import dirname, abspath +site.addsitedir(dirname(abspath(__file__))) + import sys import os import glob @@ -85,10 +91,10 @@ def pytest_addoption(parser): # test_vrf options parser.addoption("--vrf_capacity", action="store", default=None, type=int, help="vrf capacity of dut (4-1000)") parser.addoption("--vrf_test_count", action="store", default=None, type=int, help="number of vrf to be tested (1-997)") + ############################ # test_techsupport options # ############################ - parser.addoption("--loop_num", action="store", default=10, type=int, help="Change default loop range for show techsupport command") parser.addoption("--loop_delay", action="store", default=10, type=int, @@ -96,6 +102,14 @@ def pytest_addoption(parser): parser.addoption("--logs_since", action="store", type=int, help="number of minutes for show techsupport command") + ############################ + # sanity_check options # + ############################ + parser.addoption("--skip_sanity", action="store_true", default=False, + help="Skip sanity check") + parser.addoption("--allow_recover", action="store_true", default=False, + help="Allow recovery attempt in sanity check in case of failure") + @pytest.fixture(scope="session", autouse=True) def enhance_inventory(request): diff --git a/tests/platform/__init__.py b/tests/platform/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/platform/conftest.py b/tests/platform/conftest.py index d244e07a487..b24a6d32517 100644 --- a/tests/platform/conftest.py +++ b/tests/platform/conftest.py @@ -1,6 +1,6 @@ import pytest from common.fixtures.advanced_reboot import get_advanced_reboot -from args.advanced_reboot_args import add_advanced_reboot_args +from .args.advanced_reboot_args import add_advanced_reboot_args @pytest.fixture(autouse=True, scope="module") def skip_on_simx(duthost): diff --git a/tests/platform/mellanox/__init__.py b/tests/platform/mellanox/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/platform/mellanox/mellanox_thermal_control_test_helper.py b/tests/platform/mellanox/mellanox_thermal_control_test_helper.py index 212453f3dc0..eff938be694 100644 --- a/tests/platform/mellanox/mellanox_thermal_control_test_helper.py +++ b/tests/platform/mellanox/mellanox_thermal_control_test_helper.py @@ -1,7 +1,7 @@ import os import random import logging -from thermal_control_test_helper import * +from ..thermal_control_test_helper import * from common.mellanox_data import SWITCH_MODELS from minimum_table import MINIMUM_TABLE diff --git a/tests/platform/mellanox/test_thermal_control.py b/tests/platform/mellanox/test_thermal_control.py index 4eabad130be..db567bfd43f 100644 --- a/tests/platform/mellanox/test_thermal_control.py +++ b/tests/platform/mellanox/test_thermal_control.py @@ -6,7 +6,7 @@ from common.mellanox_data import SWITCH_MODELS from common.plugins.loganalyzer.loganalyzer import LogAnalyzer from common.utilities import wait_until -from thermal_control_test_helper import * +from ..thermal_control_test_helper import * from mellanox_thermal_control_test_helper import MockerHelper, AbnormalFanMocker THERMAL_CONTROL_TEST_WAIT_TIME = 65 diff --git a/tests/testbed_setup/__init__.py b/tests/testbed_setup/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/testbed_setup/conftest.py b/tests/testbed_setup/conftest.py index 71cf147fa7f..f0af95e8049 100644 --- a/tests/testbed_setup/conftest.py +++ b/tests/testbed_setup/conftest.py @@ -1,4 +1,4 @@ -from args.populate_fdb_args import add_populate_fdb_args +from .args.populate_fdb_args import add_populate_fdb_args from common.fixtures.populate_fdb import populate_fdb # FDB pytest arguments