Best Python code snippet using localstack_python
conftest.py
Source:conftest.py  
...118: from scipy._lib._fpumode import get_fpu_mode129: from scipy._lib._testutils import FPUModeChangeWarning1310: 1411: 1512: def pytest_runtest_setup(item):1613:     mark = item.get_marker("xslow")1714:     if mark is not None:1815:         try:1916:             v = int(os.environ.get('SCIPY_XSLOW', '0'))2017:         except ValueError:2118:             v = False2219:         if not v:2320:             pytest.skip("very slow test; set environment variable SCIPY_XSLOW=1 to run it")2421: 2522: 2623: @pytest.fixture(scope="function", autouse=True)2724: def check_fpu_mode(request):2825:     '''2926:     Check FPU mode was not changed during the test.3027:     '''3128:     old_mode = get_fpu_mode()3229:     yield3330:     new_mode = get_fpu_mode()3431: 3532:     if old_mode != new_mode:3633:         warnings.warn("FPU mode changed from {0:#x} to {1:#x} during "3734:                       "the test".format(old_mode, new_mode),3835:                       category=FPUModeChangeWarning, stacklevel=0)3936: 40"""41# Import the stypy library necessary elements42from stypy.type_inference_programs.type_inference_programs_imports import *43# Create the module type store44module_type_store = Context(None, __file__)45# ################# Begin of the type inference program ##################46stypy.reporting.localization.Localization.set_current(stypy.reporting.localization.Localization(__file__, 4, 0))47# 'import os' statement (line 4)48import os49import_module(stypy.reporting.localization.Localization(__file__, 4, 0), 'os', os, module_type_store)50stypy.reporting.localization.Localization.set_current(stypy.reporting.localization.Localization(__file__, 5, 0))51# 'import pytest' statement (line 5)52update_path_to_current_file_folder('C:/Python27/lib/site-packages/scipy/')53import_1 = generate_type_inference_code_for_module(stypy.reporting.localization.Localization(__file__, 5, 0), 'pytest')54if (type(import_1) is not StypyTypeError):55    if (import_1 != 'pyd_module'):56        __import__(import_1)57        sys_modules_2 = sys.modules[import_1]58        import_module(stypy.reporting.localization.Localization(__file__, 5, 0), 'pytest', sys_modules_2.module_type_store, module_type_store)59    else:60        import pytest61        import_module(stypy.reporting.localization.Localization(__file__, 5, 0), 'pytest', pytest, module_type_store)62else:63    # Assigning a type to the variable 'pytest' (line 5)64    module_type_store.set_type_of(stypy.reporting.localization.Localization(__file__, 5, 0), 'pytest', import_1)65remove_current_file_folder_from_path('C:/Python27/lib/site-packages/scipy/')66stypy.reporting.localization.Localization.set_current(stypy.reporting.localization.Localization(__file__, 6, 0))67# 'import warnings' statement (line 6)68import warnings69import_module(stypy.reporting.localization.Localization(__file__, 6, 0), 'warnings', warnings, module_type_store)70stypy.reporting.localization.Localization.set_current(stypy.reporting.localization.Localization(__file__, 8, 0))71# 'from scipy._lib._fpumode import get_fpu_mode' statement (line 8)72update_path_to_current_file_folder('C:/Python27/lib/site-packages/scipy/')73import_3 = generate_type_inference_code_for_module(stypy.reporting.localization.Localization(__file__, 8, 0), 'scipy._lib._fpumode')74if (type(import_3) is not StypyTypeError):75    if (import_3 != 'pyd_module'):76        __import__(import_3)77        sys_modules_4 = sys.modules[import_3]78        import_from_module(stypy.reporting.localization.Localization(__file__, 8, 0), 'scipy._lib._fpumode', sys_modules_4.module_type_store, module_type_store, ['get_fpu_mode'])79        nest_module(stypy.reporting.localization.Localization(__file__, 8, 0), __file__, sys_modules_4, sys_modules_4.module_type_store, module_type_store)80    else:81        from scipy._lib._fpumode import get_fpu_mode82        import_from_module(stypy.reporting.localization.Localization(__file__, 8, 0), 'scipy._lib._fpumode', None, module_type_store, ['get_fpu_mode'], [get_fpu_mode])83else:84    # Assigning a type to the variable 'scipy._lib._fpumode' (line 8)85    module_type_store.set_type_of(stypy.reporting.localization.Localization(__file__, 8, 0), 'scipy._lib._fpumode', import_3)86remove_current_file_folder_from_path('C:/Python27/lib/site-packages/scipy/')87stypy.reporting.localization.Localization.set_current(stypy.reporting.localization.Localization(__file__, 9, 0))88# 'from scipy._lib._testutils import FPUModeChangeWarning' statement (line 9)89update_path_to_current_file_folder('C:/Python27/lib/site-packages/scipy/')90import_5 = generate_type_inference_code_for_module(stypy.reporting.localization.Localization(__file__, 9, 0), 'scipy._lib._testutils')91if (type(import_5) is not StypyTypeError):92    if (import_5 != 'pyd_module'):93        __import__(import_5)94        sys_modules_6 = sys.modules[import_5]95        import_from_module(stypy.reporting.localization.Localization(__file__, 9, 0), 'scipy._lib._testutils', sys_modules_6.module_type_store, module_type_store, ['FPUModeChangeWarning'])96        nest_module(stypy.reporting.localization.Localization(__file__, 9, 0), __file__, sys_modules_6, sys_modules_6.module_type_store, module_type_store)97    else:98        from scipy._lib._testutils import FPUModeChangeWarning99        import_from_module(stypy.reporting.localization.Localization(__file__, 9, 0), 'scipy._lib._testutils', None, module_type_store, ['FPUModeChangeWarning'], [FPUModeChangeWarning])100else:101    # Assigning a type to the variable 'scipy._lib._testutils' (line 9)102    module_type_store.set_type_of(stypy.reporting.localization.Localization(__file__, 9, 0), 'scipy._lib._testutils', import_5)103remove_current_file_folder_from_path('C:/Python27/lib/site-packages/scipy/')104@norecursion105def pytest_runtest_setup(localization, *varargs, **kwargs):106    global module_type_store107    # Assign values to the parameters with defaults108    defaults = []109    # Create a new context for function 'pytest_runtest_setup'110    module_type_store = module_type_store.open_function_context('pytest_runtest_setup', 12, 0, False)111    112    # Passed parameters checking function113    pytest_runtest_setup.stypy_localization = localization114    pytest_runtest_setup.stypy_type_of_self = None115    pytest_runtest_setup.stypy_type_store = module_type_store116    pytest_runtest_setup.stypy_function_name = 'pytest_runtest_setup'117    pytest_runtest_setup.stypy_param_names_list = ['item']118    pytest_runtest_setup.stypy_varargs_param_name = None119    pytest_runtest_setup.stypy_kwargs_param_name = None120    pytest_runtest_setup.stypy_call_defaults = defaults121    pytest_runtest_setup.stypy_call_varargs = varargs122    pytest_runtest_setup.stypy_call_kwargs = kwargs123    arguments = process_argument_values(localization, None, module_type_store, 'pytest_runtest_setup', ['item'], None, None, defaults, varargs, kwargs)124    if is_error_type(arguments):125        # Destroy the current context126        module_type_store = module_type_store.close_function_context()127        return arguments128    # Initialize method data129    init_call_information(module_type_store, 'pytest_runtest_setup', localization, ['item'], arguments)130    131    # Default return type storage variable (SSA)132    # Assigning a type to the variable 'stypy_return_type'133    module_type_store.set_type_of(stypy.reporting.localization.Localization(__file__, 0, 0), 'stypy_return_type', None)134    135    136    # ################# Begin of 'pytest_runtest_setup(...)' code ##################137    138    # Assigning a Call to a Name (line 13):139    140    # Call to get_marker(...): (line 13)141    # Processing the call arguments (line 13)142    str_9 = get_builtin_python_type_instance(stypy.reporting.localization.Localization(__file__, 13, 27), 'str', 'xslow')143    # Processing the call keyword arguments (line 13)144    kwargs_10 = {}145    # Getting the type of 'item' (line 13)146    item_7 = module_type_store.get_type_of(stypy.reporting.localization.Localization(__file__, 13, 11), 'item', False)147    # Obtaining the member 'get_marker' of a type (line 13)148    get_marker_8 = module_type_store.get_type_of_member(stypy.reporting.localization.Localization(__file__, 13, 11), item_7, 'get_marker')149    # Calling get_marker(args, kwargs) (line 13)150    get_marker_call_result_11 = invoke(stypy.reporting.localization.Localization(__file__, 13, 11), get_marker_8, *[str_9], **kwargs_10)151    152    # Assigning a type to the variable 'mark' (line 13)153    module_type_store.set_type_of(stypy.reporting.localization.Localization(__file__, 13, 4), 'mark', get_marker_call_result_11)154    155    # Type idiom detected: calculating its left and rigth part (line 14)156    # Getting the type of 'mark' (line 14)157    mark_12 = module_type_store.get_type_of(stypy.reporting.localization.Localization(__file__, 14, 4), 'mark')158    # Getting the type of 'None' (line 14)159    None_13 = module_type_store.get_type_of(stypy.reporting.localization.Localization(__file__, 14, 19), 'None')160    161    (may_be_14, more_types_in_union_15) = may_not_be_none(mark_12, None_13)162    if may_be_14:163        if more_types_in_union_15:164            # Runtime conditional SSA (line 14)165            module_type_store = SSAContext.create_ssa_context(module_type_store, 'idiom if')166        else:167            module_type_store = module_type_store168        169        170        # SSA begins for try-except statement (line 15)171        module_type_store = SSAContext.create_ssa_context(module_type_store, 'try-except')172        173        # Assigning a Call to a Name (line 16):174        175        # Call to int(...): (line 16)176        # Processing the call arguments (line 16)177        178        # Call to get(...): (line 16)179        # Processing the call arguments (line 16)180        str_20 = get_builtin_python_type_instance(stypy.reporting.localization.Localization(__file__, 16, 35), 'str', 'SCIPY_XSLOW')181        str_21 = get_builtin_python_type_instance(stypy.reporting.localization.Localization(__file__, 16, 50), 'str', '0')182        # Processing the call keyword arguments (line 16)183        kwargs_22 = {}184        # Getting the type of 'os' (line 16)185        os_17 = module_type_store.get_type_of(stypy.reporting.localization.Localization(__file__, 16, 20), 'os', False)186        # Obtaining the member 'environ' of a type (line 16)187        environ_18 = module_type_store.get_type_of_member(stypy.reporting.localization.Localization(__file__, 16, 20), os_17, 'environ')188        # Obtaining the member 'get' of a type (line 16)189        get_19 = module_type_store.get_type_of_member(stypy.reporting.localization.Localization(__file__, 16, 20), environ_18, 'get')190        # Calling get(args, kwargs) (line 16)191        get_call_result_23 = invoke(stypy.reporting.localization.Localization(__file__, 16, 20), get_19, *[str_20, str_21], **kwargs_22)192        193        # Processing the call keyword arguments (line 16)194        kwargs_24 = {}195        # Getting the type of 'int' (line 16)196        int_16 = module_type_store.get_type_of(stypy.reporting.localization.Localization(__file__, 16, 16), 'int', False)197        # Calling int(args, kwargs) (line 16)198        int_call_result_25 = invoke(stypy.reporting.localization.Localization(__file__, 16, 16), int_16, *[get_call_result_23], **kwargs_24)199        200        # Assigning a type to the variable 'v' (line 16)201        module_type_store.set_type_of(stypy.reporting.localization.Localization(__file__, 16, 12), 'v', int_call_result_25)202        # SSA branch for the except part of a try statement (line 15)203        # SSA branch for the except 'ValueError' branch of a try statement (line 15)204        module_type_store.open_ssa_branch('except')205        206        # Assigning a Name to a Name (line 18):207        # Getting the type of 'False' (line 18)208        False_26 = module_type_store.get_type_of(stypy.reporting.localization.Localization(__file__, 18, 16), 'False')209        # Assigning a type to the variable 'v' (line 18)210        module_type_store.set_type_of(stypy.reporting.localization.Localization(__file__, 18, 12), 'v', False_26)211        # SSA join for try-except statement (line 15)212        module_type_store = module_type_store.join_ssa_context()213        214        215        216        # Getting the type of 'v' (line 19)217        v_27 = module_type_store.get_type_of(stypy.reporting.localization.Localization(__file__, 19, 15), 'v')218        # Applying the 'not' unary operator (line 19)219        result_not__28 = python_operator(stypy.reporting.localization.Localization(__file__, 19, 11), 'not', v_27)220        221        # Testing the type of an if condition (line 19)222        if_condition_29 = is_suitable_condition(stypy.reporting.localization.Localization(__file__, 19, 8), result_not__28)223        # Assigning a type to the variable 'if_condition_29' (line 19)224        module_type_store.set_type_of(stypy.reporting.localization.Localization(__file__, 19, 8), 'if_condition_29', if_condition_29)225        # SSA begins for if statement (line 19)226        module_type_store = SSAContext.create_ssa_context(module_type_store, 'if')227        228        # Call to skip(...): (line 20)229        # Processing the call arguments (line 20)230        str_32 = get_builtin_python_type_instance(stypy.reporting.localization.Localization(__file__, 20, 24), 'str', 'very slow test; set environment variable SCIPY_XSLOW=1 to run it')231        # Processing the call keyword arguments (line 20)232        kwargs_33 = {}233        # Getting the type of 'pytest' (line 20)234        pytest_30 = module_type_store.get_type_of(stypy.reporting.localization.Localization(__file__, 20, 12), 'pytest', False)235        # Obtaining the member 'skip' of a type (line 20)236        skip_31 = module_type_store.get_type_of_member(stypy.reporting.localization.Localization(__file__, 20, 12), pytest_30, 'skip')237        # Calling skip(args, kwargs) (line 20)238        skip_call_result_34 = invoke(stypy.reporting.localization.Localization(__file__, 20, 12), skip_31, *[str_32], **kwargs_33)239        240        # SSA join for if statement (line 19)241        module_type_store = module_type_store.join_ssa_context()242        243        if more_types_in_union_15:244            # SSA join for if statement (line 14)245            module_type_store = module_type_store.join_ssa_context()246    247    248    # ################# End of 'pytest_runtest_setup(...)' code ##################249    # Teardown call information250    teardown_call_information(localization, arguments)251    252    # Storing the return type of function 'pytest_runtest_setup' in the type store253    # Getting the type of 'stypy_return_type' (line 12)254    stypy_return_type_35 = module_type_store.get_type_of(stypy.reporting.localization.Localization(__file__, 12, 0), 'stypy_return_type')255    module_type_store.store_return_type_of_current_context(stypy_return_type_35)256    257    # Destroy the current context258    module_type_store = module_type_store.close_function_context()259    260    # Return type of the function 'pytest_runtest_setup'261    return stypy_return_type_35262# Assigning a type to the variable 'pytest_runtest_setup' (line 12)...test_raises.py
Source:test_raises.py  
...287            '*::test_mark_setup_raises_expected_exception PASSED*',288        ],289        0,290        conftest="""291            def pytest_runtest_setup(item):292                raise ValueError('the message')293        """294    )295def test_pytest_mark_setup_raises_no_args(testdir):296    _run_tests_test(testdir, """297            import pytest298            @pytest.mark.setup_raises()299            def test_mark_setup_raises_no_args():300                pass301        """,302        [303            '*::test_mark_setup_raises_no_args PASSED*',304        ],305        0,306        conftest="""307            def pytest_runtest_setup(item):308                raise RuntimeError()309        """310    )311def test_unmarked_test_setup(testdir):312    _run_tests_test(testdir, """313            def test_unmarked_test_setup():314                pass315        """,316        [317            '*::test_unmarked_test_setup ERROR*',318        ],319        1,320        conftest="""321            def pytest_runtest_setup(item):322                raise RuntimeError()323        """324    )325def test_pytest_mark_setup_raises_no_exception(testdir):326    _run_tests_test(testdir, """327            import pytest328            class DifferentException(Exception):329                pass330            @pytest.mark.setup_raises(exception = DifferentException)331            def test_pytest_mark_setup_raises_no_exception():332                pass333        """,334        [335            '*::test_pytest_mark_setup_raises_no_exception FAILED*',336            "*Expected exception <class '*.DifferentException'>, but it did not raise",337        ],338        1339    )340def test_pytest_mark_setup_raises_unexpected_exception(testdir):341    _run_tests_test(testdir, """342            import pytest343            class SomeException(Exception):344                pass345            @pytest.mark.setup_raises(exception = SomeException)346            def test_pytest_mark_setup_raises_unexpected_exception():347                pass348        """,349        [350            '*::test_pytest_mark_setup_raises_unexpected_exception FAILED*',351            # pylint: disable=line-too-long352            "*ExpectedException: Expected exception of type <class '*SomeException'>, but got exception of type <class '*AnotherException'> with message: the message",353        ],354        1,355        conftest="""356            class AnotherException(Exception):357                pass358            def pytest_runtest_setup(item):359                raise AnotherException('the message')360        """361    )362def test_pytest_mark_setup_raises_not_an_exception(testdir):363    _run_tests_test(testdir, """364            import pytest365            class NotAnException(object):366                pass367            @pytest.mark.setup_raises(exception=NotAnException)368            def test_pytest_mark_setup_raises_not_an_exception():369                pass370        """,371        [372            '*::test_pytest_mark_setup_raises_not_an_exception FAILED*',373            # pylint: disable=line-too-long374            "PytestRaisesUsageError: @pytest.mark.setup_raises: supplied `exception=<class '*NotAnException'>` is not a subclass of `BaseException`."375        ],376        1377    )378def test_pytest_mark_setup_raises_not_an_exception_class(testdir):379    _run_tests_test(testdir, """380            import pytest381            @pytest.mark.setup_raises(exception=RuntimeError('bad'))382            def test_pytest_mark_setup_raises_not_an_exception_class():383                pass384        """,385        [386            '*::test_pytest_mark_setup_raises_not_an_exception_class FAILED*',387            # pylint: disable=line-too-long388            'PytestRaisesUsageError: @pytest.mark.setup_raises: supplied `exception` argument must be a Class, e.g., `exception=RuntimeError`.'389        ],390        1391    )392def test_pytest_mark_setup_raises_unexpected_exception_fixture(testdir):393    _run_tests_test(testdir, """394            import pytest395            class SomeException(Exception):396                pass397            class AnotherException(Exception):398                pass399            @pytest.fixture400            def raise_me():401                raise AnotherException('the message')402            @pytest.mark.setup_raises(exception = SomeException)403            def test_pytest_mark_setup_raises_unexpected_exception_fixture(raise_me):404                pass405        """,406        [407            '*::test_pytest_mark_setup_raises_unexpected_exception_fixture FAILED*',408            '*AnotherException*with message: the message',409        ],410        1411    )412def test_pytest_mark_setup_raises_expected_message(testdir):413    _run_tests_test(testdir, """414            import pytest415            @pytest.mark.setup_raises(message='the message')416            def test_pytest_mark_setup_raises_expected_message():417                pass418        """,419        [420            '*::test_pytest_mark_setup_raises_expected_message PASSED*'421        ],422        0,423        conftest="""424            def pytest_runtest_setup(item):425                raise RuntimeError('the message')426        """427    )428def test_pytest_mark_setup_raises_unexpected_message(testdir):429    _run_tests_test(testdir, """430            import pytest431            @pytest.mark.setup_raises(message='the message')432            def test_pytest_mark_setup_raises_unexpected_message():433                pass434        """,435        [436            '*::test_pytest_mark_setup_raises_unexpected_message FAILED*',437            '*"the message" not in "a different message"*'438        ],439        1,440        conftest="""441            def pytest_runtest_setup(item):442                raise RuntimeError('a different message')443        """444    )445def test_pytest_mark_setup_raises_expected_match(testdir):446    _run_tests_test(testdir, """447            import pytest448            @pytest.mark.setup_raises(match=r'.*Middle.*Road.*')449            def test_pytest_mark_setup_raises_expected_match():450                pass451        """,452        [453            '*::test_pytest_mark_setup_raises_expected_match PASSED*',454        ],455        0,456        conftest="""457            def pytest_runtest_setup(item):458                raise RuntimeError('In The Middle Of The Road')459        """460    )461def test_pytest_mark_setup_raises_expected_match_with_flags(testdir):462    _run_tests_test(testdir, """463            import pytest464            import re465            @pytest.mark.setup_raises(match=r'.*middle.*road.*', match_flags=re.IGNORECASE)466            def test_pytest_mark_setup_raises_expected_match_with_flags():467                pass468        """,469        [470            '*::test_pytest_mark_setup_raises_expected_match_with_flags PASSED*',471        ],472        0,473        conftest="""474            def pytest_runtest_setup(item):475                raise RuntimeError('In The Middle Of The Road')476        """477    )478def test_pytest_mark_setup_raises_unexpected_match(testdir):479    _run_tests_test(testdir, """480            import pytest481            @pytest.mark.setup_raises(match='^middle$')482            def test_pytest_mark_setup_raises_unexpected_match():483                pass484        """,485        [486            '*::test_pytest_mark_setup_raises_unexpected_match FAILED*',487            '*ExpectedMessage: "^middle$" does not match raised message "In The Middle Of The Road"'488        ],489        1,490        conftest="""491            def pytest_runtest_setup(item):492                raise RuntimeError('In The Middle Of The Road')493        """494    )495def test_pytest_mark_setup_raises_message_and_match_fails(testdir):496    _run_tests_test(testdir, """497            import pytest498            @pytest.mark.setup_raises(message='some message', match=r'stuff')499            def test_pytest_mark_setup_raises_message_and_match_fails():500                pass501        """,502        [503            '*::test_pytest_mark_setup_raises_message_and_match_fails FAILED*',504            'PytestRaisesUsageError: @pytest.mark.setup_raises: only `message="some message"` *OR* `match="stuff"` allowed, not both.'505        ],506        1507    )508def test_pytest_mark_setup_raises_demo(testdir):509    _run_tests_test(testdir, """510            import pytest511            @pytest.mark.custom_marker(valid=False)512            @pytest.mark.setup_raises(513                exception=ValueError, match=r'.*was False$'514            )515            def test_mark_setup_raises_demo():516                pass517            @pytest.mark.custom_marker(valid=True)518            def test_all_good():519                pass520        """,521        [522            '*::test_mark_setup_raises_demo PASSED*',523            '*::test_all_good PASSED*'524        ],525        0,526        conftest="""527            def pytest_runtest_setup(item):528                custom_marker = item.get_closest_marker('custom_marker')529                if custom_marker:530                    valid = custom_marker.kwargs.get('valid', True)531                    if not valid:532                        raise ValueError('custom_marker.valid was False')533        """...pytest_raises.py
Source:pytest_raises.py  
...27       failed.28    **Parameters**29    ``item``30        The ``pytest`` test item, e.g., what is supplied to31        ``pytest_runtest_setup(item)`` or ``pytest_runtest_call(item)``.32    ``outcome``33        The ``pytest`` test outcome for the ``@pytest.hookimpl(hookwrapper=True)`` hook34        wrappers, where ``outcome = yield``.35    ``marker_name``36        The string marker name.  Values are **assumed** to be ``'setup_raises'`` or37        ``'raises'`` **only**.38        - ``'setup_raises'``: call originates from ``pytest_runtest_setup`` hook wrapper.39        - ``'raises'``: call originates from ``pytest_runtest_call`` hook wrapper.40    ``ExceptionClass``41        The exception class to re-raise.  Expected to be :class:`ExpectedException` or42        :class:`ExpectedMessage`, but not strictly required.43    ``failure_message``44        The string failure message to mark with or re-raise, depending on the value45        of ``marker_name``.46    ``traceback``47        The traceback information if available, ``None`` otherwise.48    """49    # pylint: disable=unused-variable50    __tracebackhide__ = True51    if marker_name == 'setup_raises':52        # In the later stage when `fail` is called, it is nice to "simulate" an53        # exception by putting the expected exception class's name as a prefix.54        failure_message = '{}: {}'.format(ExceptionClass.__name__, failure_message)55        item.add_marker(pytest.mark.setup_raises_expected_exc_or_message_not_found(failure_message))56    else:  # marker_name == 'raises'57        # Avoid "while handling exception another exception occurred" scenarios.58        if issubclass(ExceptionClass, PytestRaisesUsageError):59            failure_message = '{}: {}'.format(ExceptionClass.__name__, failure_message)60            pytest.fail(failure_message, pytrace=False)61        else:62            try:63                raise ExceptionClass(failure_message)64            except(ExceptionClass):65                # 1. Try and set ``outcome.excinfo``.66                # 2. Sometimes (unknown when) ``outcome.excinfo`` will trigger an67                #    AttributeError even when the test raised.  So try and set the68                #    undocumented ``outcome._excinfo`` attribute instead.69                # 3. If setting ``outcome._excinfo`` fails, fallback on ``pytest.fail``.70                excinfo = sys.exc_info()71                if traceback:72                    excinfo = excinfo[:2] + (traceback, )73                try:74                    outcome.excinfo = excinfo75                # pylint: disable=bare-except76                except:77                    try:78                        # pylint: disable=protected-access79                        outcome._excinfo = excinfo80                    # pylint: disable=bare-except81                    except:  # pragma: no cover (no tests hit this, kept for safety).82                        pytest.fail(failure_message, pytrace=False)83def _pytest_raises_validation(item, outcome, marker_name):84    """85    Validate that the test ``item`` and corresponding ``outcome`` raised an exception86    of the correct class, and if supplied the exception message was as expected.  A87    given test that has been marked with either ``@pytest.mark.setup_raises`` or88    ``@pytest.mark.raises`` can fail in one of three ways:89    1. The test raised an exception of the correct exception class, but the exception90       message did not match what was specified using either ``message`` or ``match``91       parameters.92    2. The test raised an exception of the incorrect exception class (as specified by93       the ``exception`` argument).94    3. The test was marked with either ``@pytest.mark.setup_raises`` or95       ``@pytest.mark.raises``, but no exception was raised.96    In order to support hook wrappers for both ``pytest_runtest_setup`` and97    ``pytest_runtest_call``, a "handshake" must be performed using a "secret" marker.98    This handshake is only possible because this extension implements a hook wrapper99    for both ``pytest_runtest_setup`` and ``pytest_runtest_call``.  To better explain100    the handshake, we first examine the ``pytest_runtest_call`` hook wrapper.101    ``@pytest.mark.raises(...)`` execution:102        1. The test is run (``outcome = yield``).103        2. This method is called.  If any of the three cases above that indicate failure104           happen, the test is failed.105        3. The test is failed by calling :func:`_pytest_fail_by_mark_or_set_excinfo`,106           which in this case will set ``outcome.excinfo``.107        4. By setting ``outcome.excinfo``, ``pytest`` will take over at a later stage108           and report the test as failed with our message.109    ``@pytest.mark.setup_raises(...)`` execution:110        1. The test *setup* is run (``outcome = yield``).111        2. This method is called, If any of the three cases above that indicate failure112           happen, the test is *marked* for failure.113        3. The test is failed by calling :func:`_pytest_fail_by_mark_or_set_excinfo`,114           which adds a "secret" marker that includes the failure message.115        4. Officially, the entire ``pytest_runtest_setup`` phase is completed without116           any formal failure by this extension.117        5. The ``pytest_runtest_call`` is triggered by ``pytest``, and this method is118           called again.119        6. The "secret" marker is detected, and an explicit invocation of120           ``pytest.fail`` is issued, ultimately failing the test.121    This process is unfortunately a little contrived.  However, it is done this way122    because this extension needs to be able to mark tests as failed, not error.  For123    reasons unknown to the author, any of the following issued during the124    ``pytest_runtest_setup`` hook wrapper will cause the test to **ERROR** rather than125    **FAIL**:126    - Setting ``outcome.excinfo``: during the setup phase this is a write protected127      attribute.128    - Issuing ``pytest.fail(...)``: a call to ``pytest.fail(...)`` during the setup129      phase will trigger a test **error** rather than a failure.130    .. note::131        The use of this handshake has an important implication!  Since the "secret"132        marker must be checked for first in order to fail out early, this means that133        marking a test case with **both** ``@pytest.mark.setup_raises`` and134        ``@pytest.mark.raises`` **cannot** be supported.  In practice, this should not135        be done (it does not make sense, if your setup fails you cannot run the test136        reliably).137    **Parameters**138    ``item``139        The ``pytest`` test item, e.g., what is supplied to140        ``pytest_runtest_setup(item)`` or ``pytest_runtest_call(item)``.141    ``outcome``142        The ``pytest`` test outcome for the ``@pytest.hookimpl(hookwrapper=True)`` hook143        wrappers, where ``outcome = yield``.144    ``marker_name``145        The string marker name.  Values are **assumed** to be ``'setup_raises'`` or146        ``'raises'`` **only**.147        - ``'setup_raises'``: call originates from ``pytest_runtest_setup`` hook wrapper.148        - ``'raises'``: call originates from ``pytest_runtest_call`` hook wrapper.149    """150    # pylint: disable=unused-variable151    __tracebackhide__ = True152    # Pytest 3.5+ has a new function for getting a maker from a node153    # In order to maintain compatability, prefer the newer function154    # (get_closest_marker) but use the old function (get_marker) if it155    # doesn't exist.156    marker_get_func = item.get_closest_marker if hasattr(item, 'get_closest_marker') else item.get_marker157    # Short-circuit: if the "secret" marker is found, then this test failed during setup158    # and it is now safe to ``pytest.fail`` without causing an ERROR.159    secret_marker = marker_get_func('setup_raises_expected_exc_or_message_not_found')160    if secret_marker:161        # NOTE: pytrace=False because the existing call stack is unrelated to the162        # original failure processed during `pytest_runtest_setup` hook wrapper.163        pytest.fail(secret_marker.args[0], pytrace=False)164    raises_marker = marker_get_func(marker_name)165    if raises_marker:166        exception = raises_marker.kwargs.get('exception', Exception)167        try:168            if not issubclass(exception, BaseException):169                failure_message = '@pytest.mark.{0}: supplied `exception={1}` is not a subclass of `BaseException`.'.format(170                    marker_name, exception171                )172                _pytest_fail_by_mark_or_set_excinfo(173                    item, outcome, marker_name, PytestRaisesUsageError, failure_message, None174                )175                return176        except TypeError:177            failure_message = '@pytest.mark.{}: supplied `exception` argument must be a Class, e.g., `exception=RuntimeError`.'.format(178                marker_name179            )180            _pytest_fail_by_mark_or_set_excinfo(181                item, outcome, marker_name, PytestRaisesUsageError, failure_message, None182            )183            return184        message = raises_marker.kwargs.get('message', None)185        match_pattern = raises_marker.kwargs.get('match', None)186        match_flags = raises_marker.kwargs.get('match_flags', 0)  # 0 means no flags for `re.match`187        # Only `message` or `match` should be supplied at a time, not both.188        if message and match_pattern:189            failure_message = '@pytest.mark.{}: only `message="{}"` *OR* `match="{}"` allowed, not both.'.format(190                marker_name, message, match_pattern191            )192            _pytest_fail_by_mark_or_set_excinfo(193                item, outcome, marker_name, PytestRaisesUsageError, failure_message, None194            )195            return196        raised_exception = outcome.excinfo[1] if outcome.excinfo else None197        traceback = outcome.excinfo[2] if outcome.excinfo else None198        # This plugin needs to work around the other hooks, see:199        # https://docs.pytest.org/en/latest/writing_plugins.html#hookwrapper-executing-around-other-hooks200        outcome.force_result(None)201        # Case 1: test raised exception is correct class (or derived type), check202        # message if provided by user.203        if isinstance(raised_exception, exception):204            raised_message = str(raised_exception)205            failure_message = None206            if message is not None:207                if message not in raised_message:208                    failure_message = '"{}" not in "{}"'.format(message, raised_message)209            elif match_pattern is not None:210                if not re.match(match_pattern, raised_message, match_flags):211                    failure_message = '"{}" does not match raised message "{}"'.format(match_pattern, raised_message)212            if failure_message:213                _pytest_fail_by_mark_or_set_excinfo(214                    item, outcome, marker_name, ExpectedMessage, failure_message, traceback215                )216        # Case 2: test raised exception, but it was of an unexpected type.217        elif raised_exception:218            failure_message = 'Expected exception of type {}, but got exception of type {} with message: {}'.format(219                exception, type(raised_exception), str(raised_exception)220            )221            _pytest_fail_by_mark_or_set_excinfo(222                item, outcome, marker_name, ExpectedException, failure_message, traceback223            )224        # Case 3: test did _not_ raise exception, but was expected to.225        else:226            failure_message = 'Expected exception {}, but it did not raise'.format(exception)227            _pytest_fail_by_mark_or_set_excinfo(228                item, outcome, marker_name, ExpectedException, failure_message, traceback229            )230@pytest.hookimpl(hookwrapper=True)231def pytest_runtest_setup(item):232    # pylint: disable=unused-variable233    __tracebackhide__ = True234    outcome = yield235    _pytest_raises_validation(item, outcome, 'setup_raises')236@pytest.hookimpl(hookwrapper=True)237def pytest_runtest_call(item):238    # pylint: disable=unused-variable239    __tracebackhide__ = True240    outcome = yield241    _pytest_raises_validation(item, outcome, 'raises')242# NOTE: this gets evaluated by consuming packages only.243def pytest_configure(config):  # pragma: no cover244    """245    Register the markers with pytest....pydev_runfiles_pytest2.py
Source:pydev_runfiles_pytest2.py  
...53        if isinstance(x, pytest.Item):54            try:55                # Call our setup (which may do a skip, in which56                # case we won't count it).57                pytest_runtest_setup(x)58                i += 159            except:60                continue61    State.numcollected += i62def pytest_collection_modifyitems():63    connect_to_server_for_communication_to_xml_rpc_on_xdist()64    pydev_runfiles_xml_rpc.notifyTestsCollected(State.numcollected)65    State.numcollected = 066def pytest_unconfigure(*args, **kwargs):67    _UninstallMockFileRepresentation()68    pydev_runfiles_xml_rpc.notifyTestRunFinished('Finished in: %.2f secs.' % (time.time() - State.start_time,))69def pytest_runtest_setup(item):70    filename = item.fspath.strpath71    test = item.location[2]72    State.start_test_time = time.time()73    pydev_runfiles_xml_rpc.notifyStartTest(filename, test)74def report_test(cond, filename, test, captured_output, error_contents, delta):75    '''76    @param filename: 'D:\\src\\mod1\\hello.py'77    @param test: 'TestCase.testMet1'78    @param cond: fail, error, ok79    '''80    time_str = '%.2f' % (delta,)81    pydev_runfiles_xml_rpc.notifyTest(cond, captured_output, error_contents, filename, test, time_str)82def pytest_runtest_makereport(item, call):83    report_when = call.when84    report_duration = call.stop-call.start85    excinfo = call.excinfo86    if not call.excinfo:87        report_outcome = "passed"88        report_longrepr = None89    else:90        excinfo = call.excinfo91        if not isinstance(excinfo, py.code.ExceptionInfo):92            report_outcome = "failed"93            report_longrepr = excinfo94        elif excinfo.errisinstance(py.test.skip.Exception):95            report_outcome = "skipped"96            r = excinfo._getreprcrash()97            report_longrepr = None #(str(r.path), r.lineno, r.message)98        else:99            report_outcome = "failed"100            if call.when == "call":101                report_longrepr = item.repr_failure(excinfo)102            else: # exception in setup or teardown103                report_longrepr = item._repr_failure_py(excinfo, style=item.config.option.tbstyle)104    filename = item.fspath.strpath105    test = item.location[2]106    status = 'ok'107    captured_output = ''108    error_contents = ''109    if report_outcome in ('passed', 'skipped'):110        #passed or skipped: no need to report if in setup or teardown (only on the actual test if it passed).111        if report_when in ('setup', 'teardown'):112            return113    else:114        #It has only passed, skipped and failed (no error), so, let's consider error if not on call.115        if report_when == 'setup':116            if status == 'ok':117                status = 'error'118        elif report_when == 'teardown':119            if status == 'ok':120                status = 'error'121        else:122            #any error in the call (not in setup or teardown) is considered a regular failure.123            status = 'fail'124    if call.excinfo:125        rep = report_longrepr126        if hasattr(rep, 'reprcrash'):127            reprcrash = rep.reprcrash128            error_contents += str(reprcrash)129            error_contents += '\n'130        if hasattr(rep, 'reprtraceback'):131            error_contents += str(rep.reprtraceback)132        if hasattr(rep, 'sections'):133            for name, content, sep in rep.sections:134                error_contents += sep * 40135                error_contents += name136                error_contents += sep * 40137                error_contents += '\n'138                error_contents += content139                error_contents += '\n'140    if status != 'skip': #I.e.: don't event report skips...141        report_test(status, filename, test, captured_output, error_contents, report_duration)142@pytest.mark.tryfirst143def pytest_runtest_setup(item):144    '''145    Skips tests. With xdist will be on a secondary process.146    '''147    _load_filters()148    if not py_test_accept_filter:149        return #Keep on going (nothing to filter)150    f = _NormFile(str(item.parent.fspath))151    name = item.name152    if f not in py_test_accept_filter:153        pytest.skip() # Skip the file154    accept_tests = py_test_accept_filter[f]155    if item.cls is not None:156        class_name = item.cls.__name__157    else:...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!!
