File: test_hello.py

package info (click to toggle)
pytest-datadir 1.3.1%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 184 kB
  • sloc: python: 138; makefile: 8; sh: 6
file content (67 lines) | stat: -rw-r--r-- 2,124 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
from __future__ import unicode_literals
import os


def test_read_hello(datadir):
    assert set(os.listdir(str(datadir))) == {'local_directory', 'hello.txt', 'over.txt', 'a' * 250 + '.txt'}
    with (datadir/'hello.txt').open() as fp:
        contents = fp.read()
    assert contents == 'Hello, world!\n'


def test_change_test_files(datadir, original_datadir, shared_datadir, request):
    filename = datadir / 'hello.txt'
    with filename.open('w') as fp:
        fp.write('Modified text!\n')

    original_filename = original_datadir / 'hello.txt'
    with original_filename.open() as fp:
        assert fp.read() == 'Hello, world!\n'

    with filename.open() as fp:
        assert fp.read() == 'Modified text!\n'

    shared_filename = shared_datadir/'over.txt'
    with shared_filename.open('w') as fp:
        fp.write('changed')
    shared_original = os.path.join(request.fspath.dirname, 'data', 'over.txt')
    with open(shared_original) as fp:
        assert fp.read().strip() == '8000'



def test_read_spam_from_other_dir(shared_datadir):
    filename = shared_datadir / 'spam.txt'
    with filename.open() as fp:
        contents = fp.read()
    assert contents == 'eggs\n'


def test_file_override(shared_datadir, datadir):
    """ The same file is in the module dir and global data.
        Shared files are kept in a different temp directory"""
    shared_filepath = shared_datadir/'over.txt'
    private_filepath = datadir/'over.txt'
    assert shared_filepath.is_file()
    assert private_filepath.is_file()
    assert shared_filepath != private_filepath


def test_local_directory(datadir):
    directory = datadir/'local_directory'
    assert directory.is_dir()
    filename = directory/'file.txt'
    assert filename.is_file()
    with filename.open() as fp:
        contents = fp.read()
    assert contents == 'local contents'


def test_shared_directory(shared_datadir):
    assert shared_datadir.is_dir()
    filename = shared_datadir/'shared_directory'/'file.txt'
    assert filename.is_file()
    with filename.open() as fp:
        contents = fp.read()
    assert contents == 'global contents'