Skip to content

Commit

Permalink
Replace global system test files with local ones and add tests
Browse files Browse the repository at this point in the history
Instead of using '/etc/hosts' and similar files for testing it is more
portable and 'unit test'-like to use files provided with this repository.

Add tests to verify files specified not as paths (str) but as
py.path.local are accepted as well.
  • Loading branch information
omarkohl committed Aug 1, 2015
1 parent 3b162aa commit 57e5782
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 11 deletions.
36 changes: 36 additions & 0 deletions tests/_fixture_files/huckleberry.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
CHAPTER I.

YOU don't know about me without you have read a book by the name of The
Adventures of Tom Sawyer; but that ain't no matter. That book was made by
Mr. Mark Twain, and he told the truth, mainly. There was things which he
stretched, but mainly he told the truth. That is nothing. I never seen
anybody but lied one time or another, without it was Aunt Polly, or the
widow, or maybe Mary. Aunt Polly—Tom's Aunt Polly, she is—and Mary, and
the Widow Douglas is all told about in that book, which is mostly a true
book, with some stretchers, as I said before.

Now the way that the book winds up is this: Tom and me found the money
that the robbers hid in the cave, and it made us rich. We got six
thousand dollars apiece—all gold. It was an awful sight of money when it
was piled up. Well, Judge Thatcher he took it and put it out at interest,
and it fetched us a dollar a day apiece all the year round—more than a
body could tell what to do with. The Widow Douglas she took me for her
son, and allowed she would sivilize me; but it was rough living in the
house all the time, considering how dismal regular and decent the widow
was in all her ways; and so when I couldn't stand it no longer I lit out.
I got into my old rags and my sugar-hogshead again, and was free and
satisfied. But Tom Sawyer he hunted me up and said he was going to start
a band of robbers, and I might join if I would go back to the widow and be
respectable. So I went back.

The widow she cried over me, and called me a poor lost lamb, and she
called me a lot of other names, too, but she never meant no harm by it.
She put me in them new clothes again, and I couldn't do nothing but sweat
and sweat, and feel all cramped up. Well, then, the old thing commenced
again. The widow rung a bell for supper, and you had to come to time.
When you got to the table you couldn't go right to eating, but you had to
wait for the widow to tuck down her head and grumble a little over the
victuals, though there warn't really anything the matter with them,—that
is, nothing only everything was cooked by itself. In a barrel of odds and
ends it is different; things get mixed up, and the juice kind of swaps
around, and the things go better.
Binary file added tests/_fixture_files/random.bin
Binary file not shown.
Binary file added tests/_fixture_files/sparrow.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
63 changes: 52 additions & 11 deletions tests/test_pytest_datafiles.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,64 @@
import os
import py
import pytest


@pytest.mark.datafiles("/etc/hosts")
def test_copy_hosts(datafiles):
TEST_DIR = os.path.dirname(os.path.realpath(__file__))
FIXTURE_DIR = os.path.join(TEST_DIR, '_fixture_files')
FIXTURE_FILES = [
os.path.join(FIXTURE_DIR, name)
for name in [
'huckleberry.txt',
'random.bin',
'sparrow.jpg',
]
]


@pytest.mark.datafiles(
py.path.local(
FIXTURE_FILES[0], # huckleberry.txt
)
)
def test_single_file_pypath(datafiles):
"""
Verify that a single file (py.path.local) is copied correctly
"""
assert (datafiles / 'huckleberry.txt').check(file=1)
assert 'Mark Twain' in (datafiles / 'huckleberry.txt').read_text('utf-8')


@pytest.mark.datafiles(FIXTURE_FILES[0]) # huckleberry.txt
def test_single_file_str(datafiles):
"""
Verify that a single file (str) is copied correctly
"""
assert (datafiles / 'huckleberry.txt').check(file=1)
assert 'Mark Twain' in (datafiles / 'huckleberry.txt').read_text('utf-8')


@pytest.mark.datafiles(
*[py.path.local(p) for p in FIXTURE_FILES]
)
def test_multiple_files_pypath(datafiles):
"""
Verify that a 'hosts' file containing an entry 'localhost' has been copied
Verify multiple files (py.path.local) are copied correctly
"""
assert (datafiles / 'hosts').check(file=1)
assert 'localhost' in (datafiles / 'hosts').read_text('utf-8')
assert (datafiles / 'huckleberry.txt').check(file=1)
assert (datafiles / 'random.bin').check(file=1)
assert (datafiles / 'sparrow.jpg').check(file=1)
assert len((datafiles).listdir()) == 3


@pytest.mark.datafiles("/etc/hosts", "/etc/hostname")
def test_multiple_files(datafiles):
@pytest.mark.datafiles(*FIXTURE_FILES)
def test_multiple_files_str(datafiles):
"""
Verify multiple files are found
Verify multiple files (str) are copied correctly
"""
assert (datafiles / 'hosts').check(file=1)
assert (datafiles / 'hostname').check(file=1)
assert len((datafiles).listdir()) == 2
assert (datafiles / 'huckleberry.txt').check(file=1)
assert (datafiles / 'random.bin').check(file=1)
assert (datafiles / 'sparrow.jpg').check(file=1)
assert len((datafiles).listdir()) == 3


@pytest.mark.datafiles
Expand Down

0 comments on commit 57e5782

Please sign in to comment.