File: test_tmp_path.py

package info (click to toggle)
python-pytest-run-parallel 0.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 344 kB
  • sloc: python: 2,230; makefile: 3
file content (164 lines) | stat: -rw-r--r-- 4,924 bytes parent folder | download
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import pytest

parallel_threads = [
    (1, "PASSED"),  # no parallel threads
    ("auto", "PARALLEL PASSED"),  # parallel threads
]


@pytest.mark.parametrize("parallel, passing", parallel_threads)
def test_tmp_path_is_empty(pytester: pytest.Pytester, parallel, passing):
    # ensures tmp_path is empty for each thread
    # test from (gh-109)
    pytester.makepyfile("""
        def test_tmp_path(tmp_path):
            print(tmp_path)
            assert tmp_path.exists()
            assert tmp_path.is_dir()
            assert len(list(tmp_path.iterdir())) == 0
            d = tmp_path / "sub"
            assert not d.exists()
            d.mkdir()
            assert d.exists()
    """)

    result = pytester.runpytest(f"--parallel-threads={parallel}", "-v")

    result.stdout.fnmatch_lines(
        [
            f"*::test_tmp_path {passing}*",
        ]
    )


@pytest.mark.parametrize("parallel, passing", parallel_threads)
def test_tmp_path_read_write(pytester: pytest.Pytester, parallel, passing):
    # ensures we can read/write in each tmp_path
    pytester.makepyfile("""
        def test_tmp_path(tmp_path):
            file = tmp_path / "file"
            with open(file, "w") as f:
                f.write("Hello world!")
            assert file.is_file()
            assert file.read_text() == "Hello world!"
    """)

    result = pytester.runpytest(f"--parallel-threads={parallel}", "-v")

    result.stdout.fnmatch_lines(
        [
            f"*::test_tmp_path {passing}*",
        ]
    )


@pytest.mark.parametrize("parallel, passing", parallel_threads)
def test_tmp_path_delete(pytester: pytest.Pytester, parallel, passing):
    # ensures we can delete files in each tmp_path
    pytester.makepyfile("""
        def test_tmp_path(tmp_path):
            subdir = tmp_path / "subdir"
            subdir.mkdir()
            file = subdir / "file"
            with open(file, "w") as f:
                f.write("Hello world!")
            assert file.is_file()
            file.unlink()
            assert not file.exists()
            subdir.rmdir()
            assert not subdir.exists()
    """)

    result = pytester.runpytest(f"--parallel-threads={parallel}", "-v")

    result.stdout.fnmatch_lines(
        [
            f"*::test_tmp_path {passing}*",
        ]
    )


@pytest.mark.parametrize("parallel, passing", parallel_threads)
def test_tmpdir_is_empty(pytester: pytest.Pytester, parallel, passing):
    # ensures tmpdir is empty for each thread
    pytester.makepyfile("""
        def test_tmpdir(tmpdir):
            assert tmpdir.check()
            assert tmpdir.check(dir=1)
            assert len(list(tmpdir.listdir())) == 0
            assert not tmpdir.join("sub").check()
            assert tmpdir.mkdir("sub").check()
    """)

    result = pytester.runpytest(f"--parallel-threads={parallel}", "-v")

    result.stdout.fnmatch_lines(
        [
            f"*::test_tmpdir {passing}*",
        ]
    )


@pytest.mark.parametrize("parallel, passing", parallel_threads)
def test_tmpdir_read_write(pytester: pytest.Pytester, parallel, passing):
    # ensures we can read/write in each tmpdir
    pytester.makepyfile("""
        def test_tmpdir(tmpdir):
            file = tmpdir.join("file")
            with open(file, "w") as f:
                f.write("Hello world!")
            assert file.check(file=1)
            assert file.read_text("utf-8") == "Hello world!"
    """)

    result = pytester.runpytest(f"--parallel-threads={parallel}", "-v")

    result.stdout.fnmatch_lines(
        [
            f"*::test_tmpdir {passing}*",
        ]
    )


@pytest.mark.parametrize("parallel, passing", parallel_threads)
def test_tmpdir_delete(pytester: pytest.Pytester, parallel, passing):
    # ensures we can delete files in each tmpdir
    pytester.makepyfile("""
        def test_tmpdir(tmpdir):
            subdir = tmpdir.mkdir("sub")
            file = tmpdir.join("file")
            with open(file, "w") as f:
                f.write("Hello world!")
            assert file.check(file=1)
            file.remove()
            assert not file.check()
            subdir.remove()
            assert not subdir.check()
    """)

    result = pytester.runpytest(f"--parallel-threads={parallel}", "-v")

    result.stdout.fnmatch_lines(
        [
            f"*::test_tmpdir {passing}*",
        ]
    )


@pytest.mark.parametrize("parallel, passing", parallel_threads)
def test_tmp_path_tmpdir(pytester: pytest.Pytester, parallel, passing):
    # ensures tmp_path and tmpdir can be used at the same time
    pytester.makepyfile("""
        def test_both(tmp_path, tmpdir):
            assert tmp_path.exists()
            assert tmpdir.check(dir=1)
            assert tmp_path == tmpdir
    """)

    result = pytester.runpytest(f"--parallel-threads={parallel}", "-v")

    result.stdout.fnmatch_lines(
        [
            f"*::test_both {passing}*",
        ]
    )