Python nose.tools 模块,with_setup() 实例源码

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

项目:birdseye-server    作者:DramaticLiberty    | 项目源码 | 文件源码
def teardown(self):
        pass

    # @nt.with_setup(setup, teardown)
    # def test_publish_as_configured(self):
    #     data = _randstr()
    #     nt.set_trace()
    #     ps.publish(data)
    #     mockpn.publish.assert_called_once()

    # @nt.with_setup(setup, teardown)
    # def test_publish_custom_channels(self):
    #     data = _randstr()
    #     ch = _make_channel('testChan2')
    #     self.pubsub.publish(data, channels=ch)
    #     chs = [_make_channel('testChan2'), _make_channel('testChan2')]
    #     self.pubsub.publish(data, channels=chs)
        #nt.assert_in(data, self.listener.messages)
项目:GSM-scanner    作者:yosriayed    | 项目源码 | 文件源码
def test_nose_setup(testdir):
    p = testdir.makepyfile("""
        l = []
        from nose.tools import with_setup

        @with_setup(lambda: l.append(1), lambda: l.append(2))
        def test_hello():
            assert l == [1]

        def test_world():
            assert l == [1,2]

        test_hello.setup = lambda: l.append(1)
        test_hello.teardown = lambda: l.append(2)
    """)
    result = testdir.runpytest(p, '-p', 'nose')
    result.assert_outcomes(passed=2)
项目:GSM-scanner    作者:yosriayed    | 项目源码 | 文件源码
def test_nose_setup_func_failure(testdir):
    p = testdir.makepyfile("""
        from nose.tools import with_setup

        l = []
        my_setup = lambda x: 1
        my_teardown = lambda x: 2

        @with_setup(my_setup, my_teardown)
        def test_hello():
            print (l)
            assert l == [1]

        def test_world():
            print (l)
            assert l == [1,2]

    """)
    result = testdir.runpytest(p, '-p', 'nose')
    result.stdout.fnmatch_lines([
        "*TypeError: <lambda>()*"
    ])
项目:GSM-scanner    作者:yosriayed    | 项目源码 | 文件源码
def test_nose_setup_func(testdir):
    p = testdir.makepyfile("""
        from nose.tools import with_setup

        l = []

        def my_setup():
            a = 1
            l.append(a)

        def my_teardown():
            b = 2
            l.append(b)

        @with_setup(my_setup, my_teardown)
        def test_hello():
            print (l)
            assert l == [1]

        def test_world():
            print (l)
            assert l == [1,2]

    """)
    result = testdir.runpytest(p, '-p', 'nose')
    result.assert_outcomes(passed=2)
项目:GSM-scanner    作者:yosriayed    | 项目源码 | 文件源码
def test_module_level_setup(testdir):
    testdir.makepyfile("""
        from nose.tools import with_setup
        items = {}

        def setup():
            items[1]=1

        def teardown():
            del items[1]

        def setup2():
            items[2] = 2

        def teardown2():
            del items[2]

        def test_setup_module_setup():
            assert items[1] == 1

        @with_setup(setup2, teardown2)
        def test_local_setup():
            assert items[2] == 2
            assert 1 not in items
    """)
    result = testdir.runpytest('-p', 'nose')
    result.stdout.fnmatch_lines([
        "*2 passed*",
    ])