Python pytest.yield_fixture() Examples

The following are 8 code examples of pytest.yield_fixture(). 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 also want to check out all available functions/classes of the module pytest , or try the search function .
Example #1
Source File: test_moler_test.py    From moler with 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,
                                                                                     ObserverExceptionClass):
    from moler.util.moler_test import MolerTest
    exc = ObserverExceptionClass("some error inside observer")

    class MyTest(object):
        @MolerTest.raise_background_exceptions()
        # @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
            observer.set_exception(exc)

    with pytest.raises(ExecutionException) as err:
        MyTest().method_using_observer()
    ConnectionObserver.get_unraised_exceptions() 
Example #2
Source File: common_pytest.py    From python-pytest-cases with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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)
            else:
                return pytest.fixture(**kwargs)(f)
        return _decorate 
Example #3
Source File: conftest.py    From okcupyd with 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:
                    exit_stack.enter_context(patch)
                yield
        else:
            yield

    return patch_conditionally 
Example #4
Source File: test_vcr.py    From pytest-vcr with MIT License 6 votes vote down vote up
def test_marking_whole_class(testdir):
    testdir.makepyfile("""
        import pytest

        has_cassette = False

        @pytest.yield_fixture
        def vcr_cassette(vcr_cassette):
            global has_cassette
            has_cassette = True
            yield vcr_cassette
            has_cassette = False

        @pytest.mark.vcr
        class TestClass(object):
            def test_method(self):
                assert has_cassette
    """)

    result = testdir.runpytest('-s')
    assert result.ret == 0 
Example #5
Source File: conftest.py    From guy with 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)
        else:
            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
Source File: conftest.py    From puzzle with 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,
#                     name=case_obj.name,
#                     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
Source File: test_xvfb.py    From pytest-xvfb with 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."""
    monkeypatch.delenv('DISPLAY')
    testdir.makepyfile("""
        import os
        import pytest

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

        def test_foo():
            pass
    """) 
Example #8
Source File: utils.py    From betfair.py with MIT License 5 votes vote down vote up
def response_fixture_factory(url, data=None, status=200):
    @pytest.yield_fixture
    def fixture():
        responses.add(
            responses.POST,
            url,
            status=status,
            body=json.dumps(data or {}),
            content_type='application/json',
        )
        responses.start()
        yield responses
        responses.stop()
        responses.reset()
    return fixture