summaryrefslogtreecommitdiff
path: root/testing/test_compat.py
blob: 7b2251ef6f20c01dcedde416bd0d0bf9208a160d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from __future__ import absolute_import, division, print_function
import sys

import pytest
from _pytest.compat import is_generator, get_real_func


def test_is_generator():
    def zap():
        yield

    def foo():
        pass

    assert is_generator(zap)
    assert not is_generator(foo)


def test_real_func_loop_limit():

    class Evil(object):
        def __init__(self):
            self.left = 1000

        def __repr__(self):
            return "<Evil left={left}>".format(left=self.left)

        def __getattr__(self, attr):
            if not self.left:
                raise RuntimeError('its over')
            self.left -= 1
            return self

    evil = Evil()

    with pytest.raises(ValueError):
        res = get_real_func(evil)
        print(res)


@pytest.mark.skipif(sys.version_info < (3, 4),
                    reason='asyncio available in Python 3.4+')
def test_is_generator_asyncio(testdir):
    testdir.makepyfile("""
        from _pytest.compat import is_generator
        import asyncio
        @asyncio.coroutine
        def baz():
            yield from [1,2,3]

        def test_is_generator_asyncio():
            assert not is_generator(baz)
    """)
    # avoid importing asyncio into pytest's own process,
    # which in turn imports logging (#8)
    result = testdir.runpytest_subprocess()
    result.stdout.fnmatch_lines(['*1 passed*'])


@pytest.mark.skipif(sys.version_info < (3, 5),
                    reason='async syntax available in Python 3.5+')
def test_is_generator_async_syntax(testdir):
    testdir.makepyfile("""
        from _pytest.compat import is_generator
        def test_is_generator_py35():
            async def foo():
                await foo()

            async def bar():
                pass

            assert not is_generator(foo)
            assert not is_generator(bar)
    """)
    result = testdir.runpytest()
    result.stdout.fnmatch_lines(['*1 passed*'])