@@ -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
2323files and/or directories.
2424
2525**Note about maintenance: ** This project is maintained and bug reports or pull
@@ -30,19 +30,19 @@ Features
3030--------
3131
3232This 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.
3434This means the original files are not modified and every test runs on its own
3535version 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
3939To take advantage of the *datafiles * fixture in a test function, add
4040*datafiles * as one of the test function parameters (per usual with `pytest `_
4141fixtures) and decorate the test function with *@pytest.mark.datafiles(file1,
4242file2, 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
4646will 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+
8292Usage
8393-----
8494
95+ The full code with more details for the examples can be found in `examples/ `.
96+
8597Example 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
192163This 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
246166Example 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
0 commit comments