Python pytest 模块,yield_fixture() 实例源码

我们从Python开源项目中,提取了以下11个代码示例,用于说明如何使用pytest.yield_fixture()

项目:fuel-ccp-tests    作者:openstack    | 项目源码 | 文件源码
def workload_fixture_generator(command):
    @pytest.yield_fixture
    def workload(admin_node):

        pids = set()

        def _make_workload():
            pid = admin_node.check_call('{} & echo $!'.format(
                command)).stdout_str
            # check process is running
            cmd = 'ls /proc/{}'.format(pid)
            err_msg = "Background process with pid `{}` is not found".format(
                pid)
            assert admin_node.execute(cmd).exit_code == 0, err_msg
            pids.add(pid)

        yield _make_workload

        for pid in pids:
            admin_node.execute('kill {}'.format(pid))

    return workload
项目:new-awesome    作者:starcraftman    | 项目源码 | 文件源码
def mock_input():
    """
    A fixture that mocks python's print function during test.
    """
    if sys.version_info < (3, 0):
        input_mod = '__builtin__.raw_input'
    else:
        input_mod = 'builtins.input'
    with mock.patch(input_mod) as mock_obj:
        yield mock_obj


# @pytest.yield_fixture(scope='function', autouse=True)
# def around_all_tests():
    # """
    # Executes before and after EVERY test.

    # Can be helpful for tracking bugs impacting test bed.
    # """
    # # before
    # yield
    # # after
项目:GSM-scanner    作者:yosriayed    | 项目源码 | 文件源码
def test_params_and_ids_yieldfixture(self, testdir):
        testdir.makepyfile("""
            import pytest

            @pytest.yield_fixture(params=[object(), object()],
                                  ids=['alpha', 'beta'])
            def fix(request):
                 yield request.param

            def test_foo(fix):
                assert 1
        """)
        res = testdir.runpytest('-v')
        res.stdout.fnmatch_lines([
            '*test_foo*alpha*',
            '*test_foo*beta*'])
项目:GSM-scanner    作者:yosriayed    | 项目源码 | 文件源码
def test_simple(self, testdir):
        testdir.makepyfile("""
            import pytest
            @pytest.yield_fixture
            def arg1():
                print ("setup")
                yield 1
                print ("teardown")
            def test_1(arg1):
                print ("test1 %s" % arg1)
            def test_2(arg1):
                print ("test2 %s" % arg1)
                assert 0
        """)
        result = testdir.runpytest("-s")
        result.stdout.fnmatch_lines("""
            *setup*
            *test1 1*
            *teardown*
            *setup*
            *test2 1*
            *teardown*
        """)
项目:GSM-scanner    作者:yosriayed    | 项目源码 | 文件源码
def test_scoped(self, testdir):
        testdir.makepyfile("""
            import pytest
            @pytest.yield_fixture(scope="module")
            def arg1():
                print ("setup")
                yield 1
                print ("teardown")
            def test_1(arg1):
                print ("test1 %s" % arg1)
            def test_2(arg1):
                print ("test2 %s" % arg1)
        """)
        result = testdir.runpytest("-s")
        result.stdout.fnmatch_lines("""
            *setup*
            *test1 1*
            *test2 1*
            *teardown*
        """)
项目:env-diff    作者:jacebrowning    | 项目源码 | 文件源码
def describe_do_run():

    @pytest.yield_fixture
    def config(tmpdir):
        cwd = os.getcwd()
        tmpdir.chdir()

        with Path(".env").open('w') as f:
            f.write("FOO=1")

        with Path("app.py").open('w') as f:
            f.write("os.getenv('FOO', 2)")

        yield Config.new(
            sourcefiles=[
                SourceFile(".env"),
                SourceFile("app.py"),
            ],
            environments=[
                Environment("test", command="echo FOO=3"),
            ],
        )

        os.chdir(cwd)

    def it_returns_table_data(runner, config):
        print(config.sourcefiles)
        data = do_run(config)

        expect(list(data)) == [
            ['Variable', 'File: .env', 'File: app.py', 'Environment: test'],
            ['FOO', 'FOO=1', "os.getenv('FOO', 2)", '3'],
        ]
项目:atom-hw    作者:flame0    | 项目源码 | 文件源码
def models(db):
    from tests.base.modules.model import ModelModule
    return ModelModule()


# @pytest.yield_fixture(scope='session', autouse=True)
# def mocks():
#     _mocks = [DatetimeMock()]
#     for mock in _mocks:
#         mock.start()
#     yield
#     for mock in _mocks:
#         mock.stop()
项目:GSM-scanner    作者:yosriayed    | 项目源码 | 文件源码
def test_setup_exception(self, testdir):
        testdir.makepyfile("""
            import pytest
            @pytest.yield_fixture(scope="module")
            def arg1():
                pytest.fail("setup")
                yield 1
            def test_1(arg1):
                pass
        """)
        result = testdir.runpytest("-s")
        result.stdout.fnmatch_lines("""
            *pytest.fail*setup*
            *1 error*
        """)
项目:GSM-scanner    作者:yosriayed    | 项目源码 | 文件源码
def test_teardown_exception(self, testdir):
        testdir.makepyfile("""
            import pytest
            @pytest.yield_fixture(scope="module")
            def arg1():
                yield 1
                pytest.fail("teardown")
            def test_1(arg1):
                pass
        """)
        result = testdir.runpytest("-s")
        result.stdout.fnmatch_lines("""
            *pytest.fail*teardown*
            *1 passed*1 error*
        """)
项目:GSM-scanner    作者:yosriayed    | 项目源码 | 文件源码
def test_no_yield(self, testdir):
        testdir.makepyfile("""
            import pytest
            @pytest.yield_fixture(scope="module")
            def arg1():
                return 1
            def test_1(arg1):
                pass
        """)
        result = testdir.runpytest("-s")
        result.stdout.fnmatch_lines("""
            *yield_fixture*requires*yield*
            *yield_fixture*
            *def arg1*
        """)
项目:GSM-scanner    作者:yosriayed    | 项目源码 | 文件源码
def test_doctest_module_session_fixture(self, testdir):
        """Test that session fixtures are initialized for doctest modules (#768)
        """
        # session fixture which changes some global data, which will
        # be accessed by doctests in a module
        testdir.makeconftest("""
            import pytest
            import sys

            @pytest.yield_fixture(autouse=True, scope='session')
            def myfixture():
                assert not hasattr(sys, 'pytest_session_data')
                sys.pytest_session_data = 1
                yield
                del sys.pytest_session_data
        """)
        testdir.makepyfile(foo="""
            import sys

            def foo():
              '''
              >>> assert sys.pytest_session_data == 1
              '''

            def bar():
              '''
              >>> assert sys.pytest_session_data == 1
              '''
        """)
        result = testdir.runpytest("--doctest-modules")
        result.stdout.fnmatch_lines('*2 passed*')