Best Python code snippet using pytest
plugin.py
Source:plugin.py
...34 yield35 end = time.time()36 self.durations[f"pytest_collect_file:{path}"] = end - start37 @pytest.hookimpl(hookwrapper=True, tryfirst=True)38 def pytest_itemcollected(self, item):39 start = time.time()40 yield41 end = time.time()42 self.durations[f"pytest_itemcollected\t{item.name}"] = end - start43 # === Run Test Hooks ===44 # https://docs.pytest.org/en/stable/reference.html#test-running-runtest-hooks45 @pytest.hookimpl(hookwrapper=True, tryfirst=True)46 def pytest_runtestloop(self, session):47 """Should mimic the output of the total test time reported by pytest.48 May not be necessary."""49 start = time.time()50 yield51 end = time.time()52 self.durations["pytest_runtestloop"] = end - start...
test_importerror.py
Source:test_importerror.py
...22 # logger.debug('result.capstderr %s', result.capstderr)23 # if call.when == 'call':24 # self.runner.set_test_result(self.runner.get_test_id(item), call)25 # logger.debug('pytest_runtest_makereport %s %s', item, call)26 def pytest_itemcollected(self, item):27 print('pytest_itemcollected %s' % item)28 def pytest_collectstart(self, collector):29 print('pytest_collectstart(self, collector)')30 def pytest_collectreport(self, report):31 pass32 def pytest_runtest_logreport(self, report):33 print('pytest_runtest_logreport')34 logger.debug('report %s', report)35 logger.debug('report.capstdout %s', report.capstdout)36 logger.debug('report.capstderr %s', report.capstderr)37if __name__ == '__main__':38 logging_tools.configure()39 print(sys.path)40 pytest.main(['-p', 'no:terminal', 'test_projects/test_module_a/test_feat_1.py'], plugins=[PutrPytestPlugin()])
_plugin.py
Source:_plugin.py
...7 # type: (Config) -> None8 config.addinivalue_line('markers', 'describe(what)')9 config.addinivalue_line('markers', 'it(what)')10@pytest.mark.trylast11def pytest_itemcollected(item):12 # type: (Function) -> None13 test_path = item._nodeid.rsplit('::', 1)[0]14 params = item.callspec.params if hasattr(item, 'callspec') else {}15 test_description = next(16 (marker.args[0] for marker in item.own_markers if marker.name == 'it'),17 '',18 ).format(**params)19 if not test_description:20 return21 nodes = [22 *filter(None, map(_get_describe_text, reversed(list(_iter_ancestors(item))))),23 test_description,24 ]25 item.name = f'{item.function.__name__}[[ {" â ".join(nodes)} ]]'...
conftest.py
Source:conftest.py
1import pytest2def pytest_itemcollected(item):3 """Set tests docstring into pytest report instead of class::test_name"""...
Looking for an in-depth tutorial around pytest? LambdaTest covers the detailed pytest tutorial that has everything related to the pytest, from setting up the pytest framework to automation testing. Delve deeper into pytest testing by exploring advanced use cases like parallel testing, pytest fixtures, parameterization, executing multiple test cases from a single file, and more.
Skim our below pytest tutorial playlist to get started with automation testing using the pytest framework.
https://www.youtube.com/playlist?list=PLZMWkkQEwOPlcGgDmHl8KkXKeLF83XlrP
Get 100 minutes of automation test minutes FREE!!