Python pytest.Item() Examples
The following are 30
code examples of pytest.Item().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
pytest
, or try the search function
.
Example #1
Source File: test_skipping.py From pytest with MIT License | 6 votes |
def test_mark_xfail_item(testdir): # Ensure pytest.mark.xfail works with non-Python Item testdir.makeconftest( """ import pytest class MyItem(pytest.Item): nodeid = 'foo' def setup(self): marker = pytest.mark.xfail("1 == 2", reason="Expected failure - false") self.add_marker(marker) marker = pytest.mark.xfail(True, reason="Expected failure - true") self.add_marker(marker) def runtest(self): assert False def pytest_collect_file(path, parent): return MyItem("foo", parent) """ ) result = testdir.inline_run() passed, skipped, failed = result.listoutcomes() assert not failed xfailed = [r for r in skipped if hasattr(r, "wasxfail")] assert xfailed
Example #2
Source File: sdk_diag.py From dcos-kafka-service with Apache License 2.0 | 6 votes |
def _setup_artifact_path(item: pytest.Item, artifact_name: str) -> str: """Given the pytest item and an artifact_name, Returns the path to write an artifact with that name.""" # full item.listchain() is e.g.: # - ['build', 'frameworks/template/tests/test_sanity.py', 'test_install'] # - ['build', 'tests/test_sanity.py', 'test_install'] # we want to turn both cases into: 'logs/test_sanity_py/test_install' if _testlogs_test_index > 0: # test_index is defined: get name like "05__test_placement_rules" test_name = "{:02d}__{}".format(_testlogs_test_index, item.name) else: # test_index is not defined: fall back to just "test_placement_rules" test_name = item.name output_dir = os.path.join(_test_suite_artifact_directory(item), test_name) if not os.path.isdir(output_dir): os.makedirs(output_dir) return os.path.join(output_dir, artifact_name)
Example #3
Source File: listener.py From agent-python-pytest with Apache License 2.0 | 6 votes |
def pytest_runtest_protocol(self, item): """ Adding issues id marks to the test item. :param item: Pytest.Item :return: generator object """ self._add_issue_id_marks(item) item_id = self.PyTestService.start_pytest_item(item) if PYTEST_HAS_LOGGING_PLUGIN: # This check can go away once we support pytest >= 3.3 with patching_logger_class(): with _pytest.logging.catching_logs(self._log_handler, level=self._log_level): yield else: yield # Finishing item in RP self.PyTestService.finish_pytest_item( item, item_id, self.result or 'SKIPPED', self.issue or None)
Example #4
Source File: sdk_utils.py From dcos-kafka-service with Apache License 2.0 | 6 votes |
def check_dcos_min_version_mark(item: pytest.Item) -> None: """Enforces the dcos_min_version pytest annotation, which should be used like this: @pytest.mark.dcos_min_version('1.10') def your_test_here(): ... In order for this annotation to take effect, this function must be called by a pytest_runtest_setup() hook. """ min_version_mark = item.get_closest_marker("dcos_min_version") if min_version_mark: min_version = min_version_mark.args[0] message = "Feature only supported in DC/OS {} and up".format(min_version) if "reason" in min_version_mark.kwargs: message += ": {}".format(min_version_mark.kwargs["reason"]) if dcos_version_less_than(min_version): pytest.skip(message)
Example #5
Source File: listener.py From agent-python-pytest with Apache License 2.0 | 6 votes |
def __init__(self, py_test_service, log_level=logging.NOTSET, endpoint=None): """Initialize RPReport Listener instance. :param py_test_service: PyTestServiceClass instance :param log_level: One of the 'CRITICAL', 'ERROR', 'WARNING','INFO','DEBUG', 'NOTSET' :param endpoint: Report Portal API endpoint """ # Test Item result self.PyTestService = py_test_service self.result = None self.issue = {} self._log_level = log_level if PYTEST_HAS_LOGGING_PLUGIN: self._log_handler = \ RPLogHandler(py_test_service=py_test_service, level=log_level, filter_client_logs=True, endpoint=endpoint)
Example #6
Source File: service.py From agent-python-pytest with Apache License 2.0 | 6 votes |
def _get_item_parts(item): """ Get item of parents. :param item: pytest.Item :return list of parents """ parts = [] parent = item.parent if not isinstance(parent, Instance): parts.append(parent) while True: parent = parent.parent if parent is None: break if isinstance(parent, Instance): continue if isinstance(parent, Session): break parts.append(parent) parts.reverse() return parts
Example #7
Source File: sdk_diag.py From dcos-kafka-service with Apache License 2.0 | 6 votes |
def _dump_plans(item: pytest.Item, service_name: str) -> None: """If the test had failed, writes the plan state(s) to log file(s).""" # Use brief timeouts, we just want a best-effort attempt here: plan_names = sdk_plan.list_plans(service_name, 5) for plan_name in plan_names: plan = sdk_plan.get_plan(service_name, plan_name, 5) # Include service name in plan filename, but be careful about folders... out_path = _setup_artifact_path( item, "plan_{}_{}.json".format(service_name.replace("/", "_"), plan_name) ) out_content = json.dumps(plan, indent=2) log.info("=> Writing {} ({} bytes)".format(out_path, len(out_content))) with open(out_path, "w") as f: f.write(out_content) f.write("\n") # ... and a trailing newline
Example #8
Source File: sdk_diag.py From dcos-kafka-service with Apache License 2.0 | 6 votes |
def _whitelisted_service_names(item: pytest.Item) -> Set[str]: """Returns a set of whitelisted service names configured by pytest marker diag_service_whitelist, which should be used like this: @pytest.mark.diag_service_whitelist(set('service1', 'service2')) def your_test_here(): ... Note that the diag_service_whitelist marker can be used on function, class, or module to be able to hierarchically configure the whitelist. """ if item.get_closest_marker(name='diag_service_whitelist') is None: return set() whitelisted_service_names: Set[str] = set() for mark in item.iter_markers(name='diag_service_whitelist'): whitelisted_service_names = whitelisted_service_names.union(mark.args[0]) return whitelisted_service_names
Example #9
Source File: service.py From agent-python-pytest with Apache License 2.0 | 6 votes |
def _get_item_dirs(item): """ Get directory of item. :param item: pytest.Item :return: list of dirs """ root_path = item.session.config.rootdir.strpath dir_path = item.fspath.new(basename="") rel_dir = dir_path.new(dirname=dir_path.relto(root_path), basename="", drive="") dir_list = [] for directory in rel_dir.parts(reverse=False): dir_name = directory.basename if dir_name: dir_list.append(dir_name) return dir_list
Example #10
Source File: conftest.py From dcos-kafka-service with Apache License 2.0 | 6 votes |
def pytest_runtest_setup(item: pytest.Item): """Hook to run before every test.""" # Inject header at start of test, following automatic "path/to/test_file.py::test_name": # Don't do this when running in teamcity, where it's redundant. if not teamcity.is_running_under_teamcity(): global start_time start_time = time.time() print( """ ========== ======= START: {}::{} ==========""".format( sdk_diag.get_test_suite_name(item), item.name ) ) if INTEGRATION_TEST_LOG_COLLECTION: sdk_diag.handle_test_setup(item) sdk_utils.check_dcos_min_version_mark(item)
Example #11
Source File: conftest.py From dcos-kafka-service with Apache License 2.0 | 6 votes |
def pytest_runtest_teardown(item: pytest.Item): """Hook to run after every test.""" # Inject footer at end of test, may be followed by additional teardown. # Don't do this when running in teamcity, where it's redundant. if not teamcity.is_running_under_teamcity(): global start_time duration = time.time() - start_time start_time = 0 print( """ ========== ======= END: {}::{} ({}) ==========""".format( sdk_diag.get_test_suite_name(item), item.name, sdk_utils.pretty_duration(duration) ) )
Example #12
Source File: service.py From agent-python-pytest with Apache License 2.0 | 6 votes |
def _get_item_name(test_item): """ Get name of item. :param test_item: pytest.Item :return: name """ name = test_item._rp_name if len(name) > 256: name = name[:256] test_item.warn( PytestWarning( 'Test node ID was truncated to "{}" because of name size ' 'constrains on reportportal'.format(name) ) ) return name
Example #13
Source File: test_skipping.py From pytest with MIT License | 6 votes |
def test_xfail_item(testdir): # Ensure pytest.xfail works with non-Python Item testdir.makeconftest( """ import pytest class MyItem(pytest.Item): nodeid = 'foo' def runtest(self): pytest.xfail("Expected Failure") def pytest_collect_file(path, parent): return MyItem("foo", parent) """ ) result = testdir.inline_run() passed, skipped, failed = result.listoutcomes() assert not failed xfailed = [r for r in skipped if hasattr(r, "wasxfail")] assert xfailed
Example #14
Source File: acceptance_test.py From pytest with MIT License | 6 votes |
def test_multiple_items_per_collector_byid(self, testdir): c = testdir.makeconftest( """ import pytest class MyItem(pytest.Item): def runtest(self): pass class MyCollector(pytest.File): def collect(self): return [MyItem(name="xyz", parent=self)] def pytest_collect_file(path, parent): if path.basename.startswith("conftest"): return MyCollector(path, parent) """ ) result = testdir.runpytest(c.basename + "::" + "xyz") assert result.ret == 0 result.stdout.fnmatch_lines(["*1 pass*"])
Example #15
Source File: conftest.py From pytest with MIT License | 6 votes |
def dummy_yaml_custom_test(testdir): """Writes a conftest file that collects and executes a dummy yaml test. Taken from the docs, but stripped down to the bare minimum, useful for tests which needs custom items collected. """ testdir.makeconftest( """ import pytest def pytest_collect_file(parent, path): if path.ext == ".yaml" and path.basename.startswith("test"): return YamlFile(path, parent) class YamlFile(pytest.File): def collect(self): yield YamlItem(self.fspath.basename, self) class YamlItem(pytest.Item): def runtest(self): pass """ ) testdir.makefile(".yaml", test1="")
Example #16
Source File: service.py From agent-python-pytest with Apache License 2.0 | 5 votes |
def _add_item_hier_parts_other(item_parts, item, item_type, hier_flag, report_parts, rp_name=""): """ Add item to hierarchy of parents. :param item_parts: Parent_items :param item: pytest.Item :param item_type: (SUITE, STORY, TEST, SCENARIO, STEP, BEFORE_CLASS, BEFORE_GROUPS, BEFORE_METHOD, BEFORE_SUITE, BEFORE_TEST, AFTER_CLASS, AFTER_GROUPS, AFTER_METHOD, AFTER_SUITE, AFTER_TEST) :param hier_flag: bool state :param report_parts: list of parent reports :param rp_name: report name :return: str rp_name """ for part in item_parts: if type(part) is item_type: if item_type is Module: module_path = str( item.fspath.new(dirname=rp_name, basename=part.fspath.basename, drive="")) rp_name = module_path if rp_name else module_path[1:] elif item_type in (Class, Function, UnitTestCase, TestCaseFunction): rp_name += ("::" if rp_name else "") + part.name if hier_flag: part._rp_name = rp_name rp_name = "" report_parts.append(part) return rp_name
Example #17
Source File: acceptance_test.py From pytest with MIT License | 5 votes |
def test_import_star_pytest(self, testdir): p = testdir.makepyfile( """ from pytest import * #Item #File main skip xfail """ ) result = testdir.runpython(p) assert result.ret == 0
Example #18
Source File: test_junitxml.py From pytest with MIT License | 5 votes |
def test_summing_simple(self, testdir, run_and_parse, xunit_family): testdir.makeconftest( """ import pytest def pytest_collect_file(path, parent): if path.ext == ".xyz": return MyItem(path, parent) class MyItem(pytest.Item): def __init__(self, path, parent): super(MyItem, self).__init__(path.basename, parent) self.fspath = path def runtest(self): raise ValueError(42) def repr_failure(self, excinfo): return "custom item runtest failed" """ ) testdir.tmpdir.join("myfile.xyz").write("hello") result, dom = run_and_parse(family=xunit_family) assert result.ret node = dom.find_first_by_tag("testsuite") node.assert_attr(errors=0, failures=1, skipped=0, tests=1) tnode = node.find_first_by_tag("testcase") tnode.assert_attr(name="myfile.xyz") fnode = tnode.find_first_by_tag("failure") fnode.assert_attr(message="custom item runtest failed") assert "custom item runtest failed" in fnode.toxml()
Example #19
Source File: listener.py From agent-python-pytest with Apache License 2.0 | 5 votes |
def pytest_runtest_makereport(self, item): """ Change runtest_makereport function. :param item: pytest.Item :return: None """ report = (yield).get_result() if report.longrepr: self.PyTestService.post_log( escape(report.longreprtext, False), loglevel='ERROR', ) # Defining test result if report.when == 'setup': self.result = None self.issue = {} if report.failed: self.result = 'FAILED' elif report.skipped: if self.result in (None, 'PASSED'): self.result = 'SKIPPED' else: if self.result is None: self.result = 'PASSED' # Adding test comment and issue type self._add_issue_info(item, report)
Example #20
Source File: service.py From agent-python-pytest with Apache License 2.0 | 5 votes |
def finish_pytest_item(self, test_item, item_id, status, issue=None): """ Finish pytest_item. :param test_item: test_item :param item_id: Pytest.Item :param status: an item finish status (PASSED, FAILED, STOPPED, SKIPPED, RESETED, CANCELLED, INFO, WARN) :param issue: an external system issue reference :return: None """ self._stop_if_necessary() if self.rp is None: return fta_rq = { 'end_time': timestamp(), 'status': status, 'issue': issue, 'item_id': item_id } log.debug('ReportPortal - Finish TestItem: request_body=%s', fta_rq) parts = self._item_parts[test_item] self.rp.finish_test_item(**fta_rq) while len(parts) > 0: part = parts.pop() if status == "FAILED": part._rp_result = status self._hier_parts[part]["finish_counter"] -= 1 if self._hier_parts[part]["finish_counter"] > 0: continue payload = { 'end_time': timestamp(), 'issue': issue, 'item_id': self._hier_parts[part]["item_id"], 'status': part._rp_result } log.debug('ReportPortal - End TestSuite: request_body=%s', payload) self.rp.finish_test_item(**payload)
Example #21
Source File: service.py From agent-python-pytest with Apache License 2.0 | 5 votes |
def _add_item_hier_parts_parametrize(item, report_parts, tests_parts, rp_name=""): """ Add item to hierarchy of parents with params. :param item: pytest.Item :param report_parts: Parent reports :param tests_parts: test item parts :param rp_name: name of report :return: str rp_name """ for mark in item.own_markers: if mark.name == 'parametrize': ch_index = item.nodeid.find("[") test_fullname = item.nodeid[ :ch_index if ch_index > 0 else len( item.nodeid)] test_name = item.originalname rp_name += ("::" if rp_name else "") + test_name if test_fullname in tests_parts: item_test = tests_parts[test_fullname] else: item_test = Item(test_fullname, nodeid=test_fullname, session=item.session, config=item.session.config) item_test._rp_name = rp_name item_test.obj = item.obj item_test.keywords = item.keywords item_test.own_markers = item.own_markers item_test.parent = item.parent tests_parts[test_fullname] = item_test rp_name = "" report_parts.append(item_test) break return rp_name
Example #22
Source File: test_collection.py From pytest with MIT License | 5 votes |
def get_reported_items(self, hookrec): """Return pytest.Item instances reported by the pytest_collectreport hook""" calls = hookrec.getcalls("pytest_collectreport") return [ x for call in calls for x in call.report.result if isinstance(x, pytest.Item) ]
Example #23
Source File: service.py From agent-python-pytest with Apache License 2.0 | 5 votes |
def _get_item_markers(self, item): """ Get attributes of item. :param item: pytest.Item :return: list of tags """ # Try to extract names of @pytest.mark.* decorators used for test item # and exclude those which present in rp_ignore_attributes parameter def get_marker_value(item, keyword): try: marker = item.get_closest_marker(keyword) except AttributeError: # pytest < 3.6 marker = item.keywords.get(keyword) return "{}:{}".format(keyword, marker.args[0]) \ if marker and marker.args else keyword try: get_marker = getattr(item, "get_closest_marker") except AttributeError: get_marker = getattr(item, "get_marker") attributes = [{"value": get_marker_value(item, k)} for k in item.keywords if get_marker(k) is not None and k not in self.ignored_attributes] attributes.extend([{"value": tag} for tag in item.session.config.getini('rp_tests_attributes')]) return attributes
Example #24
Source File: service.py From agent-python-pytest with Apache License 2.0 | 5 votes |
def _get_item_description(test_item): """ Get description of item. :param test_item: pytest.Item :return string description """ if isinstance(test_item, (Class, Function, Module, Item)): doc = test_item.obj.__doc__ if doc is not None: return trim_docstring(doc) if isinstance(test_item, DoctestItem): return test_item.reportinfo()[2]
Example #25
Source File: markers.py From kubetest with GNU General Public License v3.0 | 5 votes |
def rolebindings_from_marker(item: pytest.Item, namespace: str) -> List[RoleBinding]: """Create RoleBindings for the test case if the test is marked with the `pytest.mark.rolebinding` marker. Args: item: The pytest test item. namespace: The namespace of the test case. Returns: The RoleBindings that were generated from the test case markers. """ rolebindings = [] for mark in item.iter_markers(name='rolebinding'): kind = mark.args[0] name = mark.args[1] subj_kind = mark.kwargs.get('subject_kind') subj_name = mark.kwargs.get('subject_name') subj = get_custom_rbac_subject(namespace, subj_kind, subj_name) if not subj: subj = get_default_rbac_subjects(namespace) rolebindings.append(RoleBinding(client.V1RoleBinding( metadata=client.V1ObjectMeta( name=f'kubetest:{item.name}', namespace=namespace, ), role_ref=client.V1RoleRef( api_group='rbac.authorization.k8s.io', kind=kind, name=name, ), subjects=subj, ))) return rolebindings
Example #26
Source File: markers.py From kubetest with GNU General Public License v3.0 | 5 votes |
def clusterrolebindings_from_marker(item: pytest.Item, namespace: str) -> List[ClusterRoleBinding]: """Create ClusterRoleBindings for the test case if the test case is marked with the `pytest.mark.clusterrolebinding` marker. Args: item: The pytest test item. namespace: The namespace of the test case. Return: The ClusterRoleBindings which were generated from the test case markers. """ clusterrolebindings = [] for mark in item.iter_markers(name='clusterrolebinding'): name = mark.args[0] subj_kind = mark.kwargs.get('subject_kind') subj_name = mark.kwargs.get('subject_name') subj = get_custom_rbac_subject(namespace, subj_kind, subj_name) if not subj: subj = get_default_rbac_subjects(namespace) clusterrolebindings.append(ClusterRoleBinding(client.V1ClusterRoleBinding( metadata=client.V1ObjectMeta( name=f'kubetest:{item.name}', ), role_ref=client.V1RoleRef( api_group='rbac.authorization.k8s.io', kind='ClusterRole', name=name, ), subjects=subj, ))) return clusterrolebindings
Example #27
Source File: pydev_runfiles_pytest2.py From filmkodi with Apache License 2.0 | 5 votes |
def pytest_collectreport(report): i = 0 for x in report.result: if isinstance(x, pytest.Item): try: # Call our setup (which may do a skip, in which # case we won't count it). pytest_runtest_setup(x) i += 1 except: continue State.numcollected += i
Example #28
Source File: conftest.py From rst2pdf with MIT License | 5 votes |
def repr_failure(self, excinfo): """Called when self.runtest() raises an exception.""" if isinstance(excinfo.value, CompareException): return excinfo.exconly() return super(Item, self).repr_failure(excinfo)
Example #29
Source File: sdk_diag.py From dcos-kafka-service with Apache License 2.0 | 5 votes |
def _dump_mesos_state(item: pytest.Item) -> None: """Downloads state from the Mesos master and saves it to the artifact path for this test.""" for name in ["state.json", "slaves"]: r = sdk_cmd.cluster_request("GET", "/mesos/{}".format(name), raise_on_error=False) if r.ok: if name.endswith(".json"): name = name[: -len(".json")] # avoid duplicate '.json' with open(_setup_artifact_path(item, "mesos_{}.json".format(name)), "w") as f: f.write(r.text)
Example #30
Source File: terminal.py From python-netsurv with MIT License | 5 votes |
def pytest_collectreport(self, report): if report.failed: self.stats.setdefault("error", []).append(report) elif report.skipped: self.stats.setdefault("skipped", []).append(report) items = [x for x in report.result if isinstance(x, pytest.Item)] self._numcollected += len(items) if self.isatty: self.report_collect()