From d6c028d2d49eeb6c6dc170b1785033d408732fe3 Mon Sep 17 00:00:00 2001 From: Xin Wang Date: Fri, 20 Sep 2019 06:45:26 +0000 Subject: [PATCH 1/2] [pytest] Fix conftest issues * Fixed some issues in the tests/conftest.py: 1. Code style, line too long, etc. 2. Removed the code for adding the current dir to sys.path. The pytest framework has done this automatically. 3. Removed duplicated definition of testbed_devices fixture. * Add empty conftest.py files to tests/platform and tests/platform/mellanox for pytest to automatically add these folders to sys.path when needed. If just run test scripts under tests/platform/mellanox, there is issue of importing the conn_graph_facts fixture defined in the tests/platform folder. Just add a new conftest.py to tests/platform can solve this issue. Signed-off-by: Xin Wang --- tests/conftest.py | 24 +++++++++++------------- tests/platform/conftest.py | 0 tests/platform/mellanox/conftest.py | 0 3 files changed, 11 insertions(+), 13 deletions(-) create mode 100644 tests/platform/conftest.py create mode 100644 tests/platform/mellanox/conftest.py diff --git a/tests/conftest.py b/tests/conftest.py index edd251c4042..eaa1a1054e0 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -11,11 +11,6 @@ pytest_plugins = ('ptf_fixtures', 'ansible_fixtures') -# Add the tests folder to sys.path, for importing the lib package -_current_file_dir = os.path.dirname(os.path.realpath(__file__)) -if _current_file_dir not in sys.path: - sys.path.append(current_file_dir) - class TestbedInfo(object): """ @@ -36,7 +31,7 @@ def __init__(self, testbed_file): name = '' for key in line: if ('uniq-name' in key or 'conf-name' in key) and '#' in line[key]: - ### skip comment line + # skip comment line continue elif 'uniq-name' in key or 'conf-name' in key: name = line[key] @@ -53,7 +48,8 @@ def __init__(self, testbed_file): def pytest_addoption(parser): parser.addoption("--testbed", action="store", default=None, help="testbed name") parser.addoption("--testbed_file", action="store", default=None, help="testbed file name") - parser.addoption("--disable_loganalyzer", action="store_true", default=False, help="disable loganalyzer analysis for 'loganalyzer' fixture") + parser.addoption("--disable_loganalyzer", action="store_true", default=False, + help="disable loganalyzer analysis for 'loganalyzer' fixture") @pytest.fixture(scope="session") @@ -80,11 +76,12 @@ def testbed_devices(ansible_adhoc, testbed): @param testbed: Fixture for parsing testbed configuration file. @return: Return the created device objects in a dictionary """ - from common.devices import SonicHost, Localhost + from common.devices import SonicHost, Localhost, PTFHost + + devices = { + "localhost": Localhost(ansible_adhoc), + "dut": SonicHost(ansible_adhoc, testbed["dut"], gather_facts=True)} - devices = {} - devices["localhost"] = Localhost(ansible_adhoc) - devices["dut"] = SonicHost(ansible_adhoc, testbed["dut"], gather_facts=True) if "ptf" in testbed: devices["ptf"] = PTFHost(ansible_adhoc, testbed["ptf"]) @@ -124,6 +121,7 @@ def eos(): eos = yaml.safe_load(stream) return eos + @pytest.fixture(autouse=True) def loganalyzer(duthost, request): loganalyzer = LogAnalyzer(ansible_host=duthost, marker_prefix=request.node.name) @@ -133,9 +131,9 @@ def loganalyzer(duthost, request): if not request.config.getoption("--disable_loganalyzer") and "disable_loganalyzer" not in request.keywords: # Read existed common regular expressions located with legacy loganalyzer module loganalyzer.load_common_config() - # Parse syslog and process result. Raise "LogAnalyzerError" exception if: total match or expected missing match is not equal to zero + # Parse syslog and process result. Raise "LogAnalyzerError" exception if: total match or expected missing + # match is not equal to zero loganalyzer.analyze(marker) else: # Add end marker into DUT syslog loganalyzer._add_end_marker(marker) - diff --git a/tests/platform/conftest.py b/tests/platform/conftest.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/platform/mellanox/conftest.py b/tests/platform/mellanox/conftest.py new file mode 100644 index 00000000000..e69de29bb2d From 41b0338a6f1b87fee058dff292b0d25ab7fd0543 Mon Sep 17 00:00:00 2001 From: Xin Wang Date: Fri, 20 Sep 2019 18:21:17 +0800 Subject: [PATCH 2/2] Remove unnecessary comment line. --- tests/conftest.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/conftest.py b/tests/conftest.py index eaa1a1054e0..b8d1c02be83 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -31,7 +31,6 @@ def __init__(self, testbed_file): name = '' for key in line: if ('uniq-name' in key or 'conf-name' in key) and '#' in line[key]: - # skip comment line continue elif 'uniq-name' in key or 'conf-name' in key: name = line[key]