File: test_methods.py

package info (click to toggle)
python-pytest-toolbox 0.4-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 196 kB
  • sloc: python: 391; makefile: 27; sh: 6
file content (111 lines) | stat: -rw-r--r-- 2,949 bytes parent folder | download | duplicates (2)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import logging
import os

from pytest_toolbox import gettree, mktree

pytest_plugins = 'pytester'


def test_mktree(tmpdir):
    mktree(tmpdir, {
        'foo': 'bar',
        'a_dir': {
            'f': 'X'
        }
    })
    assert tmpdir.join('foo').exists()
    assert tmpdir.join('foo').read_text('utf') == 'bar'
    assert tmpdir.join('a_dir').isdir()
    assert tmpdir.join('a_dir/f').read_text('utf') == 'X'


def test_deep(tmpdir):
    mktree(tmpdir, {
        'foo/bar': {
            'f': 'X'
        }
    })
    assert tmpdir.join('foo').isdir()
    assert tmpdir.join('foo/bar').isdir()
    assert tmpdir.join('foo/bar/f').read_text('utf') == 'X'


def test_gettree(tmpdir):
    tmpdir.join('foo').write_text('bar', 'utf8')
    assert {
        'foo': 'bar'
    } == gettree(tmpdir)


def test_gettree_truncate(tmpdir):
    tmpdir.join('foo').write_text('123456789', 'utf8')
    assert {
        'foo': '12...'
    } == gettree(tmpdir, max_len=5)


def test_smart_caplog(smart_caplog):
    logger = logging.getLogger('foobar')
    logger.info('this is to info')
    logger.debug('this is to debug')

    assert 'foobar INFO: this is to info\n' == smart_caplog == str(smart_caplog)
    assert 'foobar INFO: that is to info\n' == smart_caplog(('this', 'that'))
    assert 'to info' in smart_caplog
    assert repr(smart_caplog) == "< caplog: 'foobar INFO: this is to info\\n'>"


def test_caplog_change(smart_caplog):
    smart_caplog.set_loggers(log_names=('foo',), level=logging.WARNING, fmt='%(message)s')
    logger_foo = logging.getLogger('foo')
    logger_foo.info('this is to foo info')
    logger_foo.warning('this is to foo warning')
    logger_bar = logging.getLogger('bar')
    logger_bar.info('this is to bar info')
    logger_bar.warning('this is to bar warning')

    assert 'this is to foo warning\n' == smart_caplog


def test_caplog_debug(smart_caplog):
    smart_caplog.set_different_level(foobar=logging.DEBUG)
    logger = logging.getLogger('foobar')
    logger.info('this is to info')
    logger.debug('this is to debug')

    assert 'foobar INFO: this is to info\nfoobar DEBUG: this is to debug\n' == smart_caplog


def test_tmpworkdir(tmpworkdir):
    assert os.getcwd() == str(tmpworkdir)


def test_async_tests(testdir):
    testdir.makepyfile("""\
import asyncio

async def get_4():
    return 4

async def test_async(loop):
    assert isinstance(loop, asyncio.AbstractEventLoop)
    v = await get_4()
    assert v == 4
""")
    result = testdir.runpytest('-p', 'no:sugar')
    result.assert_outcomes(passed=1)


def test_debug(testdir, capsys):
    testdir.makepyfile("""\
import logging

def test_print_logs(print_logs):
    logger = logging.getLogger('foobar')
    logger.info('this is to info')
    logger.debug('this is to debug')
""")
    result = testdir.runpytest('-p', 'no:sugar', '-s')
    result.assert_outcomes(passed=1)
    _, stderr = capsys.readouterr()
    assert 'this is to debug' in stderr