Skip to content

Commit 02969ca

Browse files
authored
Merge pull request #21 from hans-d/pathlib
use pathlib.Path
2 parents 71459d8 + bd89644 commit 02969ca

32 files changed

+639
-341
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ build/
55
dist/
66
htmlcov/
77
pytest_datafiles.egg-info/
8+
.doctrees/
9+
.idea/
10+
.venv

.travis.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
language: python
44
python:
5-
- 2.7
6-
- 3.4
7-
- 3.5
85
- 3.6
96
- 3.7
107
- 3.8
8+
- 3.9
9+
- 3.10
10+
- 3.11
1111
- pypy
1212

1313
install:

CHANGELOG.rst

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ Removed
1717
Fixed
1818
Security
1919

20+
`3.0`_
21+
~~~~~~
22+
23+
Changed
24+
25+
* BREAKING CHANGE: Using `pathlib.Path` instead of `py.path` (`#7`_)
26+
* BREAKING CHANGE: Removed support for Python 2.7 and Python 3 <= 3.6
27+
2028
`2.0.1`_
2129
~~~~~~~~
2230

@@ -74,7 +82,8 @@ Added
7482
function
7583

7684

77-
.. _`Unreleased`: https://github.com/omarkohl/pytest-datafiles/compare/2.0.1...master
85+
.. _`Unreleased`: https://github.com/omarkohl/pytest-datafiles/compare/3.0...master
86+
.. _`3.0`: https://github.com/omarkohl/pytest-datafiles/compare/2.0.1...3.0
7887
.. _`2.0.1`: https://github.com/omarkohl/pytest-datafiles/compare/2.0...2.0.1
7988
.. _`2.0`: https://github.com/omarkohl/pytest-datafiles/compare/1.0...2.0
8089
.. _`1.0`: https://github.com/omarkohl/pytest-datafiles/compare/0.2...1.0
@@ -84,6 +93,7 @@ Added
8493

8594
.. _`#1`: https://github.com/omarkohl/pytest-datafiles/issues/1
8695
.. _`#2`: https://github.com/omarkohl/pytest-datafiles/issues/2
96+
.. _`#7`: https://github.com/omarkohl/pytest-datafiles/issues/7
8797
.. _`#18`: https://github.com/omarkohl/pytest-datafiles/issues/18
8898

8999

Makefile

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
.PHONY: clean-pyc clean-build docs clean
1+
.PHONY: all clean clean-build clean-pyc clean-test lint test test-quick test-example test-all coverage dist
22

33
help:
4+
@echo "all Lint, test, check coverage and package"
45
@echo "clean Remove all build, test, coverage and Python artifacts"
56
@echo "clean-build Remove build artifacts"
67
@echo "clean-pyc Remove Python file artifacts"
78
@echo "clean-test Remove test and coverage artifacts"
89
@echo "lint Check style with Flake8 and Pylint"
9-
@echo "test Run tests quickly with Python 3.5"
10-
@echo "test-all Run tests on every Python version with tox"
10+
@echo "test Run quick tests and those for the example code"
11+
@echo "test-quick Run tests quickly (main code only)with Python 3.11"
12+
@echo "test-example Run tests for the example code"
13+
@echo "test-all Run tests on every Python version with tox and the example"
1114
@echo "coverage Check code coverage quickly with Python 3"
1215
@echo "dist Package"
1316

@@ -31,18 +34,27 @@ clean-test:
3134
rm -f .coverage
3235
rm -fr htmlcov/
3336

37+
all: lint test-all coverage dist
38+
3439
lint:
3540
tox -e lint
3641

37-
test:
38-
tox -e py35
42+
test: test-example | test-quick
43+
44+
test-quick:
45+
tox -e py311
3946

40-
test-all:
47+
test-all: test-example | test-all-env
48+
49+
test-all-env:
4150
tox
4251

52+
test-example:
53+
tox -e examples
54+
4355
coverage:
4456
tox -e coverage
45-
xdg-open htmlcov/index.html
57+
command -v xdg-open && xdg-open htmlcov/index.html || true
4658

4759
dist: clean
4860
python3 setup.py sdist

README.rst

Lines changed: 46 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@ pytest-datafiles
33
================
44

55
.. image:: https://img.shields.io/travis/omarkohl/pytest-datafiles.svg
6-
:target: https://travis-ci.org/omarkohl/pytest-datafiles
6+
:target: https://travis-ci.org/omarkohl/pytest-datafiles
77

88

99
.. image:: https://coveralls.io/repos/omarkohl/pytest-datafiles/badge.svg?branch=master&service=github
10-
:target: https://coveralls.io/github/omarkohl/pytest-datafiles?branch=master
10+
:target: https://coveralls.io/github/omarkohl/pytest-datafiles?branch=master
1111

1212

1313
.. image:: https://img.shields.io/pypi/v/pytest-datafiles.svg
14-
:target: https://pypi.python.org/pypi/pytest-datafiles
14+
:target: https://pypi.python.org/pypi/pytest-datafiles
1515

1616

1717
.. image:: https://codeclimate.com/github/omarkohl/pytest-datafiles/badges/gpa.svg
18-
:target: https://codeclimate.com/github/omarkohl/pytest-datafiles
19-
:alt: Code Climate
18+
:target: https://codeclimate.com/github/omarkohl/pytest-datafiles
19+
:alt: Code Climate
2020

2121

22-
`pytest`_ plugin to create a `tmpdir`_ containing a preconfigured set of
22+
`pytest`_ plugin to create a `tmp_path`_ containing a preconfigured set of
2323
files and/or directories.
2424

2525
**Note about maintenance:** This project is maintained and bug reports or pull
@@ -30,19 +30,19 @@ Features
3030
--------
3131

3232
This plugin allows you to specify one or several files/directories that are
33-
copied to a temporary directory (`tmpdir`_) before the execution of the test.
33+
copied to a temporary directory (`tmp_path`_) before the execution of the test.
3434
This means the original files are not modified and every test runs on its own
3535
version of the same files.
3636

37-
Files/directories can be specified either as *strings* or as *py.path* objects.
37+
Files/directories can be specified either as *strings* or as *pathlib.Path* objects.
3838

3939
To take advantage of the *datafiles* fixture in a test function, add
4040
*datafiles* as one of the test function parameters (per usual with `pytest`_
4141
fixtures) and decorate the test function with *@pytest.mark.datafiles(file1,
4242
file2, dir1, dir2, ...)*. See the examples below.
4343

44-
The *datafiles* variable in your test function is a py.path object
45-
(`tmpdir`_) where the copied files are located. Under Linux systems this
44+
The *datafiles* variable in your test function is a pathlib.Path object
45+
(`tmp_path`_) where the copied files are located. Under Linux systems this
4646
will most likely be some subdirectory of */tmp/*.
4747

4848

@@ -79,9 +79,21 @@ Installation
7979
pip install pytest-datafiles
8080
8181
82+
Upgrade to 3.0
83+
--------------
84+
85+
Version 3 now uses `tmp_path`_, resulting in `pathlib.Path` objects
86+
instead of `py.path`.
87+
88+
Your tests may need to be adjusted. In `examples/example_upgradev3.py` you see some possible
89+
variations.
90+
91+
8292
Usage
8393
-----
8494

95+
The full code with more details for the examples can be found in `examples/`.
96+
8597
Example 1
8698
~~~~~~~~~
8799

@@ -93,19 +105,12 @@ data files in your test method as follows:
93105

94106
.. code-block:: python
95107
96-
import os
97-
import pytest
108+
# more details in `examples/example_1.py`
98109
99110
@pytest.mark.datafiles('/opt/big_files/film1.mp4')
100111
def test_fast_forward(datafiles):
101-
path = str(datafiles) # Convert from py.path object to path (str)
102-
assert len(os.listdir(path)) == 1
103-
assert os.path.isfile(os.path.join(path, 'film1.mp4'))
104-
#assert some_operation(os.path.join(path, 'film1.mp4')) == expected_result
112+
# ...
105113
106-
# Using py.path syntax
107-
assert len(datafiles.listdir()) == 1
108-
assert (datafiles / 'film1.mp4').check(file=1)
109114
110115
Example 2
111116
~~~~~~~~~
@@ -117,32 +122,16 @@ modified by every test.
117122

118123
.. code-block:: python
119124
120-
import os
121-
import pytest
122-
123-
FIXTURE_DIR = os.path.join(
124-
os.path.dirname(os.path.realpath(__file__)),
125-
'test_files',
126-
)
125+
# more details in `examples/example_2.py`
127126
128127
@pytest.mark.datafiles(
129-
os.path.join(FIXTURE_DIR, 'img1.jpg'),
130-
os.path.join(FIXTURE_DIR, 'img2.jpg'),
131-
os.path.join(FIXTURE_DIR, 'img3.jpg'),
132-
)
128+
FIXTURE_DIR / 'img1.jpg',
129+
FIXTURE_DIR / 'img2.jpg',
130+
FIXTURE_DIR / 'img3.jpg',
131+
)
133132
def test_find_borders(datafiles):
134-
for img in datafiles.listdir():
135-
print(img)
136-
#assert process(img) == some_expected_value
133+
# ...
137134
138-
@pytest.mark.datafiles(
139-
os.path.join(FIXTURE_DIR, 'img4.jpg'),
140-
os.path.join(FIXTURE_DIR, 'img5.jpg'),
141-
)
142-
def test_brightness(datafiles):
143-
for img in datafiles.listdir():
144-
print(img)
145-
#assert process(img) == some_expected_value
146135
147136
Example 3
148137
~~~~~~~~~
@@ -152,36 +141,18 @@ define one decorator beforehand and apply it to every test like this example:
152141

153142
.. code-block:: python
154143
155-
import os
156-
import pytest
157-
158-
FIXTURE_DIR = os.path.join(
159-
os.path.dirname(os.path.realpath(__file__)),
160-
'test_files',
161-
)
144+
# more details in `examples/example_3.py`
162145
163146
ALL_IMGS = pytest.mark.datafiles(
164-
os.path.join(FIXTURE_DIR, 'img1.jpg'),
165-
os.path.join(FIXTURE_DIR, 'img2.jpg'),
166-
os.path.join(FIXTURE_DIR, 'img3.jpg'),
167-
os.path.join(FIXTURE_DIR, 'img4.jpg'),
168-
os.path.join(FIXTURE_DIR, 'img5.jpg'),
169-
os.path.join(FIXTURE_DIR, 'img6.jpg'),
170-
os.path.join(FIXTURE_DIR, 'img7.jpg'),
171-
os.path.join(FIXTURE_DIR, 'img8.jpg'),
172-
)
147+
FIXTURE_DIR / 'img1.jpg',
148+
FIXTURE_DIR / 'img2.jpg',
149+
FIXTURE_DIR / 'img3.jpg',
150+
)
173151
174152
@ALL_IMGS
175153
def test_something1(datafiles):
176-
for img in datafiles.listdir():
177-
print(img)
178-
#assert process(img) == some_expected_value
154+
# ...
179155
180-
@ALL_IMGS
181-
def test_something2(datafiles):
182-
for img in datafiles.listdir():
183-
print(img)
184-
#assert process(img) == some_expected_value
185156
186157
Example 4
187158
~~~~~~~~~
@@ -191,79 +162,23 @@ Imagine you have 3 directories (*dir1*, *dir2*, *dir3*) each containing the file
191162

192163
This example clarifies the options **on_duplicate** and **keep_top_dir**.
193164

194-
.. code-block:: python
195-
196-
import os
197-
import pytest
198-
199-
FIXTURE_DIR = os.path.join(
200-
os.path.dirname(os.path.realpath(__file__)),
201-
'_fixture_files',
202-
)
203-
204-
@pytest.mark.datafiles(
205-
os.path.join(FIXTURE_DIR, 'dir1'),
206-
os.path.join(FIXTURE_DIR, 'dir2'),
207-
os.path.join(FIXTURE_DIR, 'dir3'),
208-
on_duplicate='ignore',
209-
)
210-
def test_dir_ignore(datafiles):
211-
# datafiles.listdir() will list fileA and fileB originally from dir1
212-
pass
213-
214-
@pytest.mark.datafiles(
215-
os.path.join(FIXTURE_DIR, 'dir1'),
216-
os.path.join(FIXTURE_DIR, 'dir2'),
217-
os.path.join(FIXTURE_DIR, 'dir3'),
218-
on_duplicate='replace',
219-
)
220-
def test_dir_replace(datafiles):
221-
# datafiles.listdir() will list fileA and fileB originally from dir3
222-
pass
223-
224-
@pytest.mark.datafiles(
225-
os.path.join(FIXTURE_DIR, 'dir1'),
226-
os.path.join(FIXTURE_DIR, 'dir2'),
227-
os.path.join(FIXTURE_DIR, 'dir3'),
228-
# on_duplicate='exception' is the default and does not need to be
229-
# specified
230-
)
231-
def test_dir_exception(datafiles):
232-
# An exception will be raised because of duplicate filename fileA
233-
pass
234-
235-
@pytest.mark.datafiles(
236-
os.path.join(FIXTURE_DIR, 'dir1'),
237-
os.path.join(FIXTURE_DIR, 'dir2'),
238-
os.path.join(FIXTURE_DIR, 'dir3'),
239-
keep_top_dir=True,
240-
)
241-
def test_dir_keep_top_dir(datafiles):
242-
# datafiles.listdir() will list dir1, dir2 and dir3 (each containing
243-
# fileA and fileB)
244-
pass
245165

246166
Example 5
247167
~~~~~~~~~
248168

249-
You can also use a py.path object instead of str paths.
169+
You can also use a str paths.
250170

251171
.. code-block:: python
252172
253-
import os
254-
import py
255-
import pytest
256-
257-
_dir = os.path.dirname(os.path.realpath(__file__))
258-
FIXTURE_DIR = py.path.local(_dir) / 'test_files'
173+
# more details in `examples/example_5.py`
259174
260175
@pytest.mark.datafiles(
261-
FIXTURE_DIR / 'img1.jpg',
262-
FIXTURE_DIR / 'img2.jpg',
263-
FIXTURE_DIR / 'img3.jpg',
264-
)
265-
def test_fast_forward(datafiles):
266-
assert len(datafiles.listdir()) == 3
176+
os.path.join(FIXTURE_DIR, 'img1.jpg'),
177+
os.path.join(FIXTURE_DIR, 'img2.jpg'),
178+
os.path.join(FIXTURE_DIR, 'img3.jpg'),
179+
)
180+
def test_str(datafiles):
181+
# ...
267182
268183
269184
Contributing
@@ -288,7 +203,7 @@ To create and upload a new package first update the version number and then:
288203
# Create some test_example.py (e.g. with one of the examples above)
289204
test-venv/bin/pytest test_example.py
290205
# Set the git tag for final release
291-
git tag -a 2.0
206+
git tag -a 3.0
292207
git push --tags
293208
# Upload the package for final release
294209
twine upload dist/*
@@ -324,7 +239,7 @@ templates `cookiecutter-pypackage`_ and `cookiecutter-pytest-plugin`_.
324239

325240

326241
.. _`pytest`: https://docs.pytest.org/en/latest/contents.html
327-
.. _`tmpdir`: https://docs.pytest.org/en/latest/tmpdir.html
242+
.. _`tmp_path`: https://docs.pytest.org/en/latest/tmp_path.html
328243
.. _`tox`: https://tox.readthedocs.org/en/latest/
329244
.. _`MIT License`: http://opensource.org/licenses/MIT
330245
.. _`file an issue`: https://github.com/omarkohl/pytest-datafiles/issues

examples/_fixture_files/dir1/fileA

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1a

examples/_fixture_files/dir1/fileB

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1b

examples/_fixture_files/dir2/fileA

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2a

0 commit comments

Comments
 (0)