Best Python code snippet using pytest
approx.py
Source:approx.py
...30 )31 )32 return MyDocTestRunner()33@contextmanager34def temporary_verbosity(config, verbosity=0):35 original_verbosity = config.getoption("verbose")36 config.option.verbose = verbosity37 try:38 yield39 finally:40 config.option.verbose = original_verbosity41@pytest.fixture42def assert_approx_raises_regex(pytestconfig):43 def do_assert(lhs, rhs, expected_message, verbosity_level=0):44 import re45 with temporary_verbosity(pytestconfig, verbosity_level):46 with pytest.raises(AssertionError) as e:47 assert lhs == approx(rhs)48 nl = "\n"49 obtained_message = str(e.value).splitlines()[1:]50 assert len(obtained_message) == len(expected_message), (51 "Regex message length doesn't match obtained.\n"52 "Obtained:\n"53 f"{nl.join(obtained_message)}\n\n"54 "Expected regex:\n"55 f"{nl.join(expected_message)}\n\n"56 )57 for i, (obtained_line, expected_line) in enumerate(58 zip(obtained_message, expected_message)59 ):...
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!!