forked from pytest-dev/pyfakefs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpytest_doctest_test.py
43 lines (33 loc) · 1.36 KB
/
pytest_doctest_test.py
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
"""
To run these doctests, install pytest and run:
$ py.test --doctest-modules pytest_doctest_test.py
Add `-s` option to enable print statements.
"""
from __future__ import unicode_literals
def make_file_factory(func_name, fake, result):
""" Return a simple function with parametrized doctest. """
def make_file(name, content=''):
with open(name, 'w') as f:
f.write(content)
make_file.__doc__ = """
>>> import os
>>> {command}
>>> name, content = 'foo', 'bar'
>>> {func_name}(name, content)
>>> open(name).read() == content
{result}
>>> os.remove(name) # Cleanup
""".format(
command="getfixture('fs')" if fake else "pass",
func_name=func_name,
result=result)
return make_file
passes = make_file_factory('passes', fake=False, result=True)
passes_too = make_file_factory('passes_too', fake=True, result=True)
# Actually, it crashes if we try to remove the file :/
passes_too.__doc__ = passes_too.__doc__.replace('>>> os.remove(name)',
'>>> pass')
fails = make_file_factory('fails', fake=False, result=False)
# Pytest crashes when running these doctests:
# crashes = make_file_factory('crashes', fake=True, result=False)
# crashes_too = make_file_factory(') SyntaxError', fake=True, result=False)