From 6bdf0219fb39f9052134b83e72ab55ebc52a93da Mon Sep 17 00:00:00 2001 From: jyejare Date: Mon, 11 Dec 2023 13:59:08 +0530 Subject: [PATCH] Markers parsing unit test --- tests/data/test_sample.py | 14 ++++++++++++++ tests/test_collector.py | 4 ++-- tests/test_parser.py | 24 ++++++++++++++++++++++++ tests/test_source_generator.py | 11 +++++++++++ 4 files changed, 51 insertions(+), 2 deletions(-) diff --git a/tests/data/test_sample.py b/tests/data/test_sample.py index 4233131..2793f50 100644 --- a/tests/data/test_sample.py +++ b/tests/data/test_sample.py @@ -1,8 +1,11 @@ # encoding=utf-8 """Sample test module.""" import unittest +import pytest +pytestmark = [pytest.mark.run_in_one_thread, pytest.mark.tier1] + CONSTANT = 'contant-value' @@ -72,3 +75,14 @@ def test_method(self): def test_without_docstring(self): # noqa: D102 pass + + +@pytest.mark.on_prem_provisioning +class TestclasswithMarkers: + """Class to verify tests markers are collected from class.""" + + @pytest.mark.skipif(2 == 3, reason='2 is not 3') + @pytest.mark.osp + def test_markers_sample(self): + """Test for markers at test level.""" + assert True diff --git a/tests/test_collector.py b/tests/test_collector.py index bfec3da..5c8f4c1 100644 --- a/tests/test_collector.py +++ b/tests/test_collector.py @@ -11,7 +11,7 @@ def test_collect_tests(path): """Check if ``collect_tests`` 'tests/data'collect tests.""" tests = collector.collect_tests(path) assert 'tests/data/test_sample.py' in tests - assert len(tests['tests/data/test_sample.py']) == 4 + assert len(tests['tests/data/test_sample.py']) == 5 # Check if we are not doing a specific python module collection if path.endswith('.py'): @@ -30,7 +30,7 @@ def test_collect_ignore_path(ignore_path): tests = collector.collect_tests('tests/data', [ignore_path]) assert 'tests/data/ignore_dir/test_ignore_dir.py' not in tests assert 'tests/data/test_sample.py' in tests - assert len(tests['tests/data/test_sample.py']) == 4 + assert len(tests['tests/data/test_sample.py']) == 5 @pytest.mark.parametrize('filename', ('test_module.py', 'module_test.py')) diff --git a/tests/test_parser.py b/tests/test_parser.py index b05ac49..f6f7829 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -83,3 +83,27 @@ def test_parse_rst_special_characters(): u'

String with special character like é

\n' u'\n' ) + + +def test_parse_markers(): + """ + Test if the markers list is parsed. + + List should be comma separated list of markers from all levels after + removing 'pytest.mark' text and ignore some markers. + """ + _mod_markers = ['pytest.mark.e2e', 'pytest.mark.destructive'] + _class_markers = [ + 'pytest.mark.on_prem_provisioning', + "pytest.mark.usefixtures('cleandir')" + ] + _test_markers = [ + "pytest.mark.parametrize('something', ['a', 'b'])", + 'pytest.mark.skipif(not settings.robottelo.REPOS_HOSTING_URL)', + 'pytest.mark.tier1' + ] + _all_markers = [_mod_markers, _class_markers, _test_markers] + + expected = 'e2e, destructive, on_prem_provisioning, tier1' + + assert parser.parse_markers(_all_markers) == expected diff --git a/tests/test_source_generator.py b/tests/test_source_generator.py index dab73dc..b369853 100644 --- a/tests/test_source_generator.py +++ b/tests/test_source_generator.py @@ -37,3 +37,14 @@ def test_source_generator(): '(lambda v: (v if v else None))' ')', ] + + +def test_source_markers(): + """Verifies if the test collection collects test markers.""" + tests = collector.collect_tests('tests/data/test_sample.py') + marked_test = [ + test for test in tests['tests/data/test_sample.py'] + if test.name == 'test_markers_sample' + ].pop() + assert marked_test.fields['markers'] == ('run_in_one_thread, tier1, ' + 'on_prem_provisioning, osp')