Best Python code snippet using pytest
__init__.py
Source:__init__.py
...96 to protect later % formatting.97 The result can be formatted by util.format_explanation() for98 pretty printing.99 """100 hook_result = item.ihook.pytest_assertrepr_compare(101 config=item.config, op=op, left=left, right=right102 )103 for new_expl in hook_result:104 if new_expl:105 new_expl = truncate.truncate_if_required(new_expl, item)106 new_expl = [line.replace("\n", "\\n") for line in new_expl]107 res = six.text_type("\n~").join(new_expl)108 if item.config.getvalue("assertmode") == "rewrite":109 res = res.replace("%", "%%")110 return res111 util._reprcompare = callbinrepr112def pytest_runtest_teardown(item):113 util._reprcompare = None114def pytest_sessionfinish(session):...
test_plugin.py
Source:test_plugin.py
...46 }),47 'urls': int48 })49 _ = expected == TEST_DATA50 msgs = pytest_assertrepr_compare('==', expected, TEST_DATA)51 assert S(Unordered([52 "failed due to validation error(s):",53 "- info.platform: not a valid value (actual: 'unix')",54 "- info.description: length of value must be at most 10 (actual: 'lorem ipsum lorem ipsum')",55 "- info.downloads: expected list (actual: {'last_month': 0})",56 "- info.classifiers: expected dict (actual: ['Development Status :: 6 - Mature', 'Intended Audience :: Developers'])",57 "- urls: expected int (actual: [{}, {}])",58 Any(59 "- releases: extra keys not allowed (actual: {'3.0.7': [], '3.1.3': []})",60 "- releases: extra keys not allowed (actual: {'3.1.3': [], '3.0.7': []})",61 ),62 ])) == msgs63 # TODO: How to assert the actual output?64 # assert expected == resp.json()65 # from _pytest.capture import capfd66 # out, err = capfd.readouterr()67 # assert out == \68 # "assert failed due to validation error(s):\n"69 # "- info.platform: not a valid value\n"70 # "- info.description: length of value must be at most 10\n"71 # "- info.downloads: expected list\n"72 # "- info.classifiers: expected dict\n"73 # "- urls: expected int\n"74 # "- releases: extra keys not allowed"75 # ]76def test_unordered():77 actual = ["foobar", "barbaz", "baz"]78 expected = S(Unordered(["foo", "bar", "baz"]))79 _ = expected == actual80 msgs = pytest_assertrepr_compare('==', expected, actual)81 assert S(Unordered([82 "failed due to validation error(s):",83 "- Element #0 (foobar) is not valid against any validator",84 "- Element #1 (barbaz) is not valid against any validator",85 ])) == msgs86def test_list_error_reporting():87 # OK: List of objects88 sch = S({'foo': [int]})89 data = {'foo': ['a', 'b']}90 assert (sch == data) is False91 assert len(sch.error.errors) == 292 assert sch.error.errors[0].path == ['foo', 0]93 assert sch.error.errors[1].path == ['foo', 1]94 msgs = pytest_assertrepr_compare('==', sch, data)95 assert msgs == [96 "failed due to validation error(s):",97 "- foo.0: expected int (actual: 'a')",98 "- foo.1: expected int (actual: 'b')"99 ]100 sch = S({'foo': [{'id': int}]})101 data = {'foo': [{'id': 'bar'}, {'id': 'bar2'}]}102 assert (sch == data) is False103 assert len(sch.error.errors) == 1 # XXX: Until https://github.com/alecthomas/voluptuous/pull/330 gets merged104 assert sch.error.errors[0].path == ['foo', 0, 'id']105 msgs = pytest_assertrepr_compare('==', sch, data)106 assert msgs == [107 "failed due to validation error(s):",108 "- foo.0.id: expected int (actual: 'bar')"...
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!!