File: test_boxed.py

package info (click to toggle)
pytest-forked 1.6.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 220 kB
  • sloc: python: 283; makefile: 6; sh: 4
file content (72 lines) | stat: -rw-r--r-- 1,657 bytes parent folder | download | duplicates (3)
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
import os

import pytest

needsfork = pytest.mark.skipif(not hasattr(os, "fork"), reason="os.fork required")


@needsfork
def test_functional_boxed(testdir):
    p1 = testdir.makepyfile(
        """
        import os
        def test_function():
            os.kill(os.getpid(), 15)
    """
    )
    result = testdir.runpytest(p1, "--forked")
    result.stdout.fnmatch_lines(["*CRASHED*", "*1 failed*"])


@needsfork
def test_functional_boxed_per_test(testdir):
    p1 = testdir.makepyfile(
        """
        import os
        import pytest

        @pytest.mark.forked
        def test_function():
            os.kill(os.getpid(), 15)
    """
    )
    result = testdir.runpytest(p1)
    result.stdout.fnmatch_lines(["*CRASHED*", "*1 failed*"])


@needsfork
@pytest.mark.parametrize(
    "capmode",
    [
        "no",
        pytest.param("sys", marks=pytest.mark.xfail(reason="capture cleanup needed")),
        pytest.param("fd", marks=pytest.mark.xfail(reason="capture cleanup needed")),
    ],
)
def test_functional_boxed_capturing(testdir, capmode):
    p1 = testdir.makepyfile(
        """
        import os
        import sys
        def test_function():
            sys.stdout.write("hello\\n")
            sys.stderr.write("world\\n")
            os.kill(os.getpid(), 15)
    """
    )
    result = testdir.runpytest(p1, "--forked", "--capture=%s" % capmode)
    result.stdout.fnmatch_lines(
        """
        *CRASHED*
        *stdout*
        hello
        *stderr*
        world
        *1 failed*
"""
    )


def test_is_not_boxed_by_default(testdir):
    config = testdir.parseconfig(testdir.tmpdir)
    assert not config.option.forked