Best Python code snippet using tavern
test_test.py
Source:test_test.py  
1# fixture and parameter have the same name2# pylint: disable=redefined-outer-name3import json4import os5from contextlib import contextmanager6from pathlib import Path7from unittest.mock import Mock, patch8import pytest9from rpdk.core.cli import EXIT_UNHANDLED_EXCEPTION, main10from rpdk.core.contract.interface import Action11from rpdk.core.exceptions import SysExitRecommendedError12from rpdk.core.project import ARTIFACT_TYPE_MODULE, ARTIFACT_TYPE_RESOURCE, Project13from rpdk.core.test import (14    DEFAULT_ENDPOINT,15    DEFAULT_FUNCTION,16    DEFAULT_REGION,17    _validate_sam_args,18    empty_override,19    get_inputs,20    get_marker_options,21    get_overrides,22    temporary_ini_file,23)24RANDOM_INI = "pytest_SOYPKR.ini"25EMPTY_OVERRIDE = empty_override()26ROLE_ARN = "role_arn"27CREDENTIALS = {28    "AccessKeyId": object(),29    "SecretAccessKey": object(),30    "SessionToken": object(),31}32SCHEMA = {"handlers": {action.lower(): [] for action in Action}}33@pytest.fixture34def base(tmpdir):35    return Path(tmpdir)36@contextmanager37def mock_temporary_ini_file():38    yield RANDOM_INI39def create_input_file(base, create_string, update_string, invalid_string):40    path = base / "inputs"41    os.mkdir(path, mode=0o777)42    path_create = path / "inputs_1_create.json"43    with path_create.open("w", encoding="utf-8") as f:44        f.write(create_string)45    path_update = path / "inputs_1_update.json"46    with path_update.open("w", encoding="utf-8") as f:47        f.write(update_string)48    path_invalid = path / "inputs_1_invalid.json"49    with path_invalid.open("w", encoding="utf-8") as f:50        f.write(invalid_string)51def create_invalid_input_file(base):52    path = base / "inputs"53    os.mkdir(path, mode=0o777)54    path_create = path / "inputs_1_test.json"55    with path_create.open("w", encoding="utf-8") as f:56        f.write('{"a": 1}')57@pytest.mark.parametrize(58    "args_in,pytest_args,plugin_args",59    [60        ([], [], [DEFAULT_FUNCTION, DEFAULT_ENDPOINT, DEFAULT_REGION, "30"]),61        (["--endpoint", "foo"], [], [DEFAULT_FUNCTION, "foo", DEFAULT_REGION, "30"]),62        (63            ["--function-name", "bar", "--enforce-timeout", "60"],64            [],65            ["bar", DEFAULT_ENDPOINT, DEFAULT_REGION, "60"],66        ),67        (68            ["--", "-k", "create"],69            ["-k", "create"],70            [DEFAULT_FUNCTION, DEFAULT_ENDPOINT, DEFAULT_REGION, "30"],71        ),72        (73            ["--region", "us-west-2", "--", "--collect-only"],74            ["--collect-only"],75            [DEFAULT_FUNCTION, DEFAULT_ENDPOINT, "us-west-2", "30"],76        ),77    ],78)79def test_test_command_happy_path(80    base, capsys, args_in, pytest_args, plugin_args81):  # pylint: disable=too-many-locals82    create_input_file(base, '{"a": 1}', '{"a": 2}', '{"b": 1}')83    mock_project = Mock(spec=Project)84    mock_project.schema = SCHEMA85    mock_project.root = base86    mock_project.executable_entrypoint = None87    mock_project.artifact_type = ARTIFACT_TYPE_RESOURCE88    patch_project = patch(89        "rpdk.core.test.Project", autospec=True, return_value=mock_project90    )91    patch_plugin = patch("rpdk.core.test.ContractPlugin", autospec=True)92    patch_client = patch("rpdk.core.test.ResourceClient", autospec=True)93    patch_pytest = patch("rpdk.core.test.pytest.main", autospec=True, return_value=0)94    patch_ini = patch(95        "rpdk.core.test.temporary_ini_file", side_effect=mock_temporary_ini_file96    )97    # fmt: off98    with patch_project, \99            patch_plugin as mock_plugin, \100            patch_client as mock_client, \101            patch_pytest as mock_pytest, \102            patch_ini as mock_ini:103        main(args_in=["test"] + args_in)104    # fmt: on105    mock_project.load.assert_called_once_with()106    function_name, endpoint, region, enforce_timeout = plugin_args107    mock_client.assert_called_once_with(108        function_name,109        endpoint,110        region,111        mock_project.schema,112        EMPTY_OVERRIDE,113        {"CREATE": {"a": 1}, "UPDATE": {"a": 2}, "INVALID": {"b": 1}},114        None,115        enforce_timeout,116        mock_project.type_name,117        None,118        None,119        None,120        None,121    )122    mock_plugin.assert_called_once_with(mock_client.return_value)123    mock_ini.assert_called_once_with()124    mock_pytest.assert_called_once_with(125        ["-c", RANDOM_INI, "-m", ""] + pytest_args, plugins=[mock_plugin.return_value]126    )127    _out, err = capsys.readouterr()128    assert not err129def test_test_command_return_code_on_error():130    mock_project = Mock(spec=Project)131    mock_project.root = None132    mock_project.schema = SCHEMA133    mock_project.executable_entrypoint = None134    mock_project.artifact_type = ARTIFACT_TYPE_RESOURCE135    patch_project = patch(136        "rpdk.core.test.Project", autospec=True, return_value=mock_project137    )138    patch_plugin = patch("rpdk.core.test.ContractPlugin", autospec=True)139    patch_client = patch("rpdk.core.test.ResourceClient", autospec=True)140    patch_pytest = patch("rpdk.core.test.pytest.main", autospec=True, return_value=1)141    with patch_project, patch_plugin, patch_client, patch_pytest:142        with pytest.raises(SystemExit) as excinfo:143            main(args_in=["test"])144    assert excinfo.value.code != EXIT_UNHANDLED_EXCEPTION145def test_test_command_module_project_succeeds():146    mock_project = Mock(spec=Project)147    mock_project.artifact_type = ARTIFACT_TYPE_MODULE148    patch_project = patch(149        "rpdk.core.test.Project", autospec=True, return_value=mock_project150    )151    with patch_project:152        main(args_in=["test"])153def test_temporary_ini_file():154    with temporary_ini_file() as path_str:155        assert isinstance(path_str, str)156        path = Path(path_str)157        assert path.name.startswith("pytest_")158        assert path.name.endswith(".ini")159        with path.open("r", encoding="utf-8") as f:160            assert "[pytest]" in f.read()161def test_get_overrides_no_root():162    assert get_overrides(None, DEFAULT_REGION, "", None) == EMPTY_OVERRIDE163def test_get_overrides_file_not_found(base):164    path = base / "overrides.json"165    try:166        path.unlink()167    except FileNotFoundError:168        pass169    assert get_overrides(path, DEFAULT_REGION, "", None) == EMPTY_OVERRIDE170def test_get_overrides_invalid_file(base):171    path = base / "overrides.json"172    path.write_text("{}")173    assert get_overrides(base, DEFAULT_REGION, "", None) == EMPTY_OVERRIDE174def test_get_overrides_empty_overrides(base):175    path = base / "overrides.json"176    with path.open("w", encoding="utf-8") as f:177        json.dump(EMPTY_OVERRIDE, f)178    assert get_overrides(base, DEFAULT_REGION, "", None) == EMPTY_OVERRIDE179def test_get_overrides_invalid_pointer_skipped(base):180    overrides = empty_override()181    overrides["CREATE"]["#/foo/bar"] = None182    path = base / "overrides.json"183    with path.open("w", encoding="utf-8") as f:184        json.dump(overrides, f)185    assert get_overrides(base, DEFAULT_REGION, "", None) == EMPTY_OVERRIDE186def test_get_overrides_good_path(base):187    overrides = empty_override()188    overrides["CREATE"]["/foo/bar"] = {}189    path = base / "overrides.json"190    with path.open("w", encoding="utf-8") as f:191        json.dump(overrides, f)192    assert get_overrides(base, DEFAULT_REGION, "", None) == {193        "CREATE": {("foo", "bar"): {}}194    }195@pytest.mark.parametrize(196    "overrides_string,list_exports_return_value,expected_overrides",197    [198        (199            '{"CREATE": {"/foo/bar": "{{TestInvalidExport}}"}}',200            [{"Exports": [{"Value": "TestValue", "Name": "Test"}]}],201            empty_override(),202        ),203        (204            '{"CREATE": {"/foo/bar": {{TestExport}}}}',205            [{"Exports": [{"Value": 5, "Name": "TestExport"}]}],206            {"CREATE": {("foo", "bar"): 5}},207        ),208        (209            '{"CREATE": {"/foo/bar": "{{TestExport}}"}}',210            [211                {"Exports": [{"Value": "FirstTestValue", "Name": "FirstTestExport"}]},212                {"Exports": [{"Value": "TestValue", "Name": "TestExport"}]},213            ],214            {"CREATE": {("foo", "bar"): "TestValue"}},215        ),216        (217            '{"CREATE": {"/foo/bar": "{{TestExport}}",'218            + ' "/foo/bar2": "{{TestInvalidExport}}"}}',219            [{"Exports": [{"Value": "TestValue", "Name": "TestExport"}]}],220            empty_override(),221        ),222    ],223)224def test_get_overrides_with_jinja(225    base, overrides_string, list_exports_return_value, expected_overrides226):227    mock_sts_client = Mock(spec=["get_session_token"])228    mock_cfn_client = Mock(spec=["get_paginator"])229    mock_paginator = Mock(spec=["paginate"])230    mock_cfn_client.get_paginator.return_value = mock_paginator231    mock_paginator.paginate.return_value = list_exports_return_value232    mock_sts_client.get_session_token.return_value = CREDENTIALS233    patch_sdk = patch("rpdk.core.test.create_sdk_session", autospec=True)234    path = base / "overrides.json"235    with path.open("w", encoding="utf-8") as f:236        f.write(overrides_string)237    with patch_sdk as mock_sdk:238        mock_sdk.return_value.region_name = "us-east-1"239        mock_sdk.return_value.client.side_effect = [240            mock_sts_client,241            mock_cfn_client,242            Mock(),243        ]244        result = get_overrides(base, DEFAULT_REGION, None, None)245    assert result == expected_overrides246@pytest.mark.parametrize(247    "schema,expected_marker_keywords",248    [249        (SCHEMA, ""),250        (251            {"handlers": {"create": [], "read": [], "update": [], "delete": []}},252            ("not list",),253        ),254        (255            {"handlers": {"create": []}},256            ("not read", "not update", "not delete", "not list", " and "),257        ),258    ],259)260def test_get_marker_options(schema, expected_marker_keywords):261    marker_options = get_marker_options(schema)262    assert all(keyword in marker_options for keyword in expected_marker_keywords)263@pytest.mark.parametrize(264    "create_string,update_string,invalid_string,"265    "list_exports_return_value,expected_inputs",266    [267        (268            '{"Name": "TestName"}',269            '{"Name": "TestNameNew"}',270            '{"Name": "TestNameNew"}',271            [{"Exports": [{"Value": "TestValue", "Name": "Test"}]}],272            {273                "CREATE": {"Name": "TestName"},274                "UPDATE": {"Name": "TestNameNew"},275                "INVALID": {"Name": "TestNameNew"},276            },277        )278    ],279)280# pylint: disable=R0913281# pylint: disable=R0914282def test_with_inputs(283    base,284    create_string,285    update_string,286    invalid_string,287    list_exports_return_value,288    expected_inputs,289):290    mock_sts_client = Mock(spec=["get_session_token"])291    mock_sts_client.get_session_token.return_value = CREDENTIALS292    mock_cfn_client = Mock(spec=["get_paginator"])293    mock_paginator = Mock(spec=["paginate"])294    mock_cfn_client.get_paginator.return_value = mock_paginator295    mock_paginator.paginate.return_value = list_exports_return_value296    patch_sdk = patch("rpdk.core.test.create_sdk_session", autospec=True)297    create_input_file(base, create_string, update_string, invalid_string)298    with patch_sdk as mock_sdk:299        mock_sdk.return_value.region_name = "us-east-1"300        mock_sdk.return_value.client.side_effect = [301            mock_sts_client,302            mock_cfn_client,303            Mock(),304        ]305        result = get_inputs(base, DEFAULT_REGION, None, 1, None)306    assert result == expected_inputs307def test_with_inputs_invalid(base):308    mock_sts_client = Mock(spec=["get_session_token"])309    mock_sts_client.get_session_token.return_value = CREDENTIALS310    mock_cfn_client = Mock(spec=["get_paginator"])311    mock_paginator = Mock(spec=["paginate"])312    mock_cfn_client.get_paginator.return_value = mock_paginator313    mock_paginator.paginate.return_value = (314        '[{"Exports": [{"Value": "TestValue", "Name": "Test"}]}]'315    )316    patch_sdk = patch("rpdk.core.test.create_sdk_session", autospec=True)317    create_invalid_input_file(base)318    with patch_sdk as mock_sdk:319        mock_sdk.return_value.region_name = "us-east-1"320        mock_sdk.return_value.client.side_effect = [321            mock_sts_client,322            mock_cfn_client,323            Mock(),324        ]325        result = get_inputs(base, DEFAULT_REGION, None, 1, None)326    assert not result327def test_get_input_invalid_root():328    assert not get_inputs("", DEFAULT_REGION, "", 1, None)329def test_get_input_input_folder_does_not_exist(base):330    assert not get_inputs(base, DEFAULT_REGION, "", 1, None)331def test_get_input_file_not_found(base):332    path = base / "inputs"333    os.mkdir(path, mode=0o777)334    assert not get_inputs(base, DEFAULT_REGION, "", 1, None)335def test_use_both_sam_and_docker_arguments():336    args = Mock(spec_set=["docker_image", "endpoint"])337    args.docker_image = "image"338    args.endpoint = "endpoint"339    try:340        _validate_sam_args(args)341    except SysExitRecommendedError as e:342        assert (343            "Cannot specify both --docker-image and --endpoint or --function-name"344            in str(e)...test_call_run.py
Source:test_call_run.py  
2import pytest3from tavern.core import run4from tavern.util import exceptions5@pytest.fixture(autouse=True)6def patch_pytest():7    with patch("tavern.core.pytest.main") as fake_main:8        yield9    assert fake_main.called10class TestBasicRun:11    def test_run(self):12        run("")13    def test_run_with_empty_cfg(self):14        run("", {})15    def test_run_with_cfg(self):16        run("", {"a": 2})17    @pytest.mark.parametrize(18        "expected_kwarg",19        ("tavern_mqtt_backend", "tavern_http_backend", "tavern_strict"),20    )21    def test_doesnt_warn_about_expected_kwargs(self, expected_kwarg):22        kw = {expected_kwarg: 123}23        with pytest.warns(None) as warn_rec:24            run("", **kw)25        assert not len(warn_rec)26class TestParseGlobalCfg:27    def test_path_correct(self):28        run("", tavern_global_cfg=__file__)29    def test_pass_dict(self):30        run("", tavern_global_cfg={"variables": {"a": 1}})31class TestParseFailures:32    @pytest.fixture(autouse=True)33    def patch_pytest(self):34        with patch("tavern.core.pytest.main") as fake_main:35            yield36        assert not fake_main.called37    def test_path_nonexistent(self):38        with pytest.raises(exceptions.InvalidSettingsError):39            run("", tavern_global_cfg="sdfsdd")40    def test_bad_type(self):41        with pytest.raises(exceptions.InvalidSettingsError):...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
