-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathtest_tmpdir.py
More file actions
executable file
·46 lines (34 loc) · 1.65 KB
/
test_tmpdir.py
File metadata and controls
executable file
·46 lines (34 loc) · 1.65 KB
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
def test_tmpdir(tmpdir):
# tmpdir already has a path name associated with it
# join() extends the path to include a filename
# the file is created when it's written to
a_file = tmpdir.join("something.txt")
# you can create directories
a_sub_dir = tmpdir.mkdir("anything")
# you can create files in directories (created when written)
another_file = a_sub_dir.join("something_else.txt")
# this write creates 'something.txt'
a_file.write("contents may settle during shipping")
# this write creates 'anything/something_else.txt'
another_file.write("something different")
# you can read the files as well
assert a_file.read() == "contents may settle during shipping"
assert another_file.read() == "something different"
def test_tmpdir_factory(tmpdir_factory):
# you should start with making a directory
# a_dir acts like the object returned from the tmpdir fixture
a_dir = tmpdir_factory.mktemp("mydir")
# base_temp will be the parent dir of 'mydir'
# you don't have to use getbasetemp()
# using it here just to show that it's available
base_temp = tmpdir_factory.getbasetemp()
print("base:", base_temp)
# the rest of this test looks the same as the 'test_tmpdir()'
# example except I'm using a_dir instead of tmpdir
a_file = a_dir.join("something.txt")
a_sub_dir = a_dir.mkdir("anything")
another_file = a_sub_dir.join("something_else.txt")
a_file.write("contents may settle during shipping")
another_file.write("something different")
assert a_file.read() == "contents may settle during shipping"
assert another_file.read() == "something different"