Python pytest.yield_fixture() Examples

The following are 8 code examples for showing how to use pytest.yield_fixture(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module pytest , or try the search function .

Example 1
Project: moler   Author: nokia   File:    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_exception_in_observer_is_raised_if_no_result_called_but_decorator_on_method(do_nothing_connection_observer,
    from moler.util.moler_test import MolerTest
    exc = ObserverExceptionClass("some error inside observer")

    class MyTest(object):
        # @MolerTest.raise_background_exceptions  # doesn't work since it is created by python and given class as first argument
        #                                               # compare with syntax of @pytest.fixture  @pytest.yield_fixture
        def method_using_observer(self):
            observer = do_nothing_connection_observer

    with pytest.raises(ExecutionException) as err:
Example 2
def pytest_fixture(hook=None, name=None, **kwargs):
        """Generator-aware pytest.fixture decorator for legacy pytest versions"""
        def _decorate(f):
            if name is not None:
                # 'name' argument is not supported in this old version, use the __name__ trick.
                f.__name__ = name

            # call hook if needed
            if hook is not None:
                f = hook(f)

            # create the fixture
            if isgeneratorfunction(f):
                return pytest.yield_fixture(**kwargs)(f)
                return pytest.fixture(**kwargs)(f)
        return _decorate 
Example 3
Project: okcupyd   Author: IvanMalison   File:    License: MIT License 6 votes vote down vote up
def patch(option, *patches, **kwargs):
    negate = kwargs.get('negate', False)

    @pytest.yield_fixture(autouse=True, scope='session')
    def patch_conditionally(request):
        condition = bool(request.config.getoption(option))
        if negate:
            condition = not condition
        if condition:
            with contextlib.ExitStack() as exit_stack:
                for patch in patches:

    return patch_conditionally 
Example 4
Project: pytest-vcr   Author: ktosiek   File:    License: MIT License 6 votes vote down vote up
def test_marking_whole_class(testdir):
        import pytest

        has_cassette = False

        def vcr_cassette(vcr_cassette):
            global has_cassette
            has_cassette = True
            yield vcr_cassette
            has_cassette = False

        class TestClass(object):
            def test_method(self):
                assert has_cassette

    result = testdir.runpytest('-s')
    assert result.ret == 0 
Example 5
Project: guy   Author: manatlan   File:    License: Apache License 2.0 6 votes vote down vote up
def runner(request):
    def _( ga, **kargs ):
        time.sleep(0.5) # leave the time to shutdown previous instance
        if request.param=="serve":
            return getattr(ga,request.param)(port=10000)
            return getattr(ga,request.param)(**kargs)

    return _

# @pytest.yield_fixture(scope='session')
# def event_loop(request):
#     """Create an instance of the default event loop for each test case."""
#     loop = asyncio.get_event_loop_policy().new_event_loop()
#     yield loop
#     loop.close() 
Example 6
Project: puzzle   Author: robinandeer   File:    License: MIT License 6 votes vote down vote up
def case_obj(ped_lines):
    """Return a test case object with individuals."""
    _case = get_cases('tests/fixtures/hapmap.vcf', case_lines=ped_lines)[0]
    yield _case

# @pytest.yield_fixture(scope='function')
# def sql_case_obj(case_obj):
#     """Return a test case for the sql model."""
#     _sql_case = SqlCase(case_id=case_obj.case_id,
#           ,
#                     variant_source=case_obj.variant_source,
#                     variant_type=case_obj.variant_type,
#                     variant_mode=case_obj.variant_type,
#                     compressed=case_obj.compressed,
#                     tabix_index=case_obj.tabix_index)
#     yield _sql_case 
Example 7
Project: pytest-xvfb   Author: The-Compiler   File:    License: MIT License 5 votes vote down vote up
def test_early_display(monkeypatch, testdir):
    """Make sure DISPLAY is set in a session-scoped fixture already."""
        import os
        import pytest

        @pytest.yield_fixture(scope='session', autouse=True)
        def fixt():
            assert 'DISPLAY' in os.environ

        def test_foo():
Example 8
Project:   Author: jmcarp   File:    License: MIT License 5 votes vote down vote up
def response_fixture_factory(url, data=None, status=200):
    def fixture():
            body=json.dumps(data or {}),
        yield responses
    return fixture