Skip to content

Commit d609b63

Browse files
Merge pull request #3528 from asottile/blacken_docs
blacken docs
2 parents 4dc5f78 + 0f2d7dc commit d609b63

19 files changed

+228
-130
lines changed

.pre-commit-config.yaml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@ repos:
33
- repo: https://github.com/ambv/black
44
rev: 18.4a4
55
hooks:
6-
- id: black
7-
args: [--safe, --quiet]
8-
python_version: python3.6
6+
- id: black
7+
args: [--safe, --quiet]
8+
language_version: python3.6
9+
- repo: https://github.com/asottile/blacken-docs
10+
rev: v0.1.1
11+
hooks:
12+
- id: blacken-docs
13+
additional_dependencies: [black==18.5b1]
14+
language_version: python3.6
915
- repo: https://github.com/pre-commit/pre-commit-hooks
1016
rev: v1.2.3
1117
hooks:

README.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ An example of a simple test:
4040
def inc(x):
4141
return x + 1
4242
43+
4344
def test_answer():
4445
assert inc(3) == 5
4546

doc/en/bash-completion.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Install argcomplete using::
1414

1515
For global activation of all argcomplete enabled python applications run::
1616

17-
sudo activate-global-python-argcomplete
17+
sudo activate-global-python-argcomplete
1818

1919
For permanent (but not global) ``pytest`` activation, use::
2020

doc/en/capture.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ an example test function that performs some output related checks:
9292

9393
.. code-block:: python
9494
95-
def test_myoutput(capsys): # or use "capfd" for fd-level
95+
def test_myoutput(capsys): # or use "capfd" for fd-level
9696
print("hello")
9797
sys.stderr.write("world\n")
9898
captured = capsys.readouterr()
@@ -145,9 +145,9 @@ as a context manager, disabling capture inside the ``with`` block:
145145
.. code-block:: python
146146
147147
def test_disabling_capturing(capsys):
148-
print('this output is captured')
148+
print("this output is captured")
149149
with capsys.disabled():
150-
print('output not captured, going directly to sys.stdout')
151-
print('this output is also captured')
150+
print("output not captured, going directly to sys.stdout")
151+
print("this output is also captured")
152152
153153
.. include:: links.inc

doc/en/example/attic.rst

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,18 @@ example: specifying and selecting acceptance tests
99
# ./conftest.py
1010
def pytest_option(parser):
1111
group = parser.getgroup("myproject")
12-
group.addoption("-A", dest="acceptance", action="store_true",
13-
help="run (slow) acceptance tests")
12+
group.addoption(
13+
"-A", dest="acceptance", action="store_true", help="run (slow) acceptance tests"
14+
)
15+
1416

1517
def pytest_funcarg__accept(request):
1618
return AcceptFixture(request)
1719

20+
1821
class AcceptFixture(object):
1922
def __init__(self, request):
20-
if not request.config.getoption('acceptance'):
23+
if not request.config.getoption("acceptance"):
2124
pytest.skip("specify -A to run acceptance tests")
2225
self.tmpdir = request.config.mktemp(request.function.__name__, numbered=True)
2326

@@ -61,6 +64,7 @@ extend the `accept example`_ by putting this in our test module:
6164
arg.tmpdir.mkdir("special")
6265
return arg
6366

67+
6468
class TestSpecialAcceptance(object):
6569
def test_sometest(self, accept):
6670
assert accept.tmpdir.join("special").check()

doc/en/example/simple.rst

Lines changed: 62 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ Here is a basic pattern to achieve this:
1818
# content of test_sample.py
1919
def test_answer(cmdopt):
2020
if cmdopt == "type1":
21-
print ("first")
21+
print("first")
2222
elif cmdopt == "type2":
23-
print ("second")
24-
assert 0 # to see what was printed
23+
print("second")
24+
assert 0 # to see what was printed
2525
2626
2727
For this to work we need to add a command line option and
@@ -32,9 +32,12 @@ provide the ``cmdopt`` through a :ref:`fixture function <fixture function>`:
3232
# content of conftest.py
3333
import pytest
3434
35+
3536
def pytest_addoption(parser):
36-
parser.addoption("--cmdopt", action="store", default="type1",
37-
help="my option: type1 or type2")
37+
parser.addoption(
38+
"--cmdopt", action="store", default="type1", help="my option: type1 or type2"
39+
)
40+
3841
3942
@pytest.fixture
4043
def cmdopt(request):
@@ -102,9 +105,12 @@ the command line arguments before they get processed:
102105
103106
# content of conftest.py
104107
import sys
108+
109+
105110
def pytest_load_initial_conftests(args):
106-
if 'xdist' in sys.modules: # pytest-xdist plugin
111+
if "xdist" in sys.modules: # pytest-xdist plugin
107112
import multiprocessing
113+
108114
num = max(multiprocessing.cpu_count() / 2, 1)
109115
args[:] = ["-n", str(num)] + args
110116
@@ -136,9 +142,13 @@ line option to control skipping of ``pytest.mark.slow`` marked tests:
136142
# content of conftest.py
137143
138144
import pytest
145+
146+
139147
def pytest_addoption(parser):
140-
parser.addoption("--runslow", action="store_true",
141-
default=False, help="run slow tests")
148+
parser.addoption(
149+
"--runslow", action="store_true", default=False, help="run slow tests"
150+
)
151+
142152
143153
def pytest_collection_modifyitems(config, items):
144154
if config.getoption("--runslow"):
@@ -206,10 +216,13 @@ Example:
206216
207217
# content of test_checkconfig.py
208218
import pytest
219+
220+
209221
def checkconfig(x):
210222
__tracebackhide__ = True
211223
if not hasattr(x, "config"):
212-
pytest.fail("not configured: %s" %(x,))
224+
pytest.fail("not configured: %s" % (x,))
225+
213226
214227
def test_something():
215228
checkconfig(42)
@@ -240,13 +253,16 @@ this to make sure unexpected exception types aren't hidden:
240253
import operator
241254
import pytest
242255
256+
243257
class ConfigException(Exception):
244258
pass
245259
260+
246261
def checkconfig(x):
247-
__tracebackhide__ = operator.methodcaller('errisinstance', ConfigException)
262+
__tracebackhide__ = operator.methodcaller("errisinstance", ConfigException)
248263
if not hasattr(x, "config"):
249-
raise ConfigException("not configured: %s" %(x,))
264+
raise ConfigException("not configured: %s" % (x,))
265+
250266
251267
def test_something():
252268
checkconfig(42)
@@ -269,22 +285,28 @@ running from a test you can do something like this:
269285
270286
# content of conftest.py
271287
288+
272289
def pytest_configure(config):
273290
import sys
291+
274292
sys._called_from_test = True
275293
294+
276295
def pytest_unconfigure(config):
277296
import sys
297+
278298
del sys._called_from_test
279299
280300
and then check for the ``sys._called_from_test`` flag:
281301

282302
.. code-block:: python
283303
284-
if hasattr(sys, '_called_from_test'):
304+
if hasattr(sys, "_called_from_test"):
285305
# called from within a test run
306+
...
286307
else:
287308
# called "normally"
309+
...
288310
289311
accordingly in your application. It's also a good idea
290312
to use your own application module rather than ``sys``
@@ -301,6 +323,7 @@ It's easy to present extra information in a ``pytest`` run:
301323
302324
# content of conftest.py
303325
326+
304327
def pytest_report_header(config):
305328
return "project deps: mylib-1.1"
306329
@@ -325,8 +348,9 @@ display more information if applicable:
325348
326349
# content of conftest.py
327350
351+
328352
def pytest_report_header(config):
329-
if config.getoption('verbose') > 0:
353+
if config.getoption("verbose") > 0:
330354
return ["info1: did you know that ...", "did you?"]
331355
332356
which will add info only when run with "--v"::
@@ -367,12 +391,15 @@ out which tests are the slowest. Let's make an artificial test suite:
367391
# content of test_some_are_slow.py
368392
import time
369393
394+
370395
def test_funcfast():
371396
time.sleep(0.1)
372397
398+
373399
def test_funcslow1():
374400
time.sleep(0.2)
375401
402+
376403
def test_funcslow2():
377404
time.sleep(0.3)
378405
@@ -409,17 +436,19 @@ an ``incremental`` marker which is to be used on classes:
409436
410437
import pytest
411438
439+
412440
def pytest_runtest_makereport(item, call):
413441
if "incremental" in item.keywords:
414442
if call.excinfo is not None:
415443
parent = item.parent
416444
parent._previousfailed = item
417445
446+
418447
def pytest_runtest_setup(item):
419448
if "incremental" in item.keywords:
420449
previousfailed = getattr(item.parent, "_previousfailed", None)
421450
if previousfailed is not None:
422-
pytest.xfail("previous test failed (%s)" %previousfailed.name)
451+
pytest.xfail("previous test failed (%s)" % previousfailed.name)
423452
424453
These two hook implementations work together to abort incremental-marked
425454
tests in a class. Here is a test module example:
@@ -430,15 +459,19 @@ tests in a class. Here is a test module example:
430459
431460
import pytest
432461
462+
433463
@pytest.mark.incremental
434464
class TestUserHandling(object):
435465
def test_login(self):
436466
pass
467+
437468
def test_modification(self):
438469
assert 0
470+
439471
def test_deletion(self):
440472
pass
441473
474+
442475
def test_normal():
443476
pass
444477
@@ -489,9 +522,11 @@ Here is an example for making a ``db`` fixture available in a directory:
489522
# content of a/conftest.py
490523
import pytest
491524
525+
492526
class DB(object):
493527
pass
494528
529+
495530
@pytest.fixture(scope="session")
496531
def db():
497532
return DB()
@@ -600,6 +635,7 @@ case we just write some information out to a ``failures`` file:
600635
import pytest
601636
import os.path
602637
638+
603639
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
604640
def pytest_runtest_makereport(item, call):
605641
# execute all other hooks to obtain the report object
@@ -626,6 +662,8 @@ if you then have failing tests:
626662
# content of test_module.py
627663
def test_fail1(tmpdir):
628664
assert 0
665+
666+
629667
def test_fail2():
630668
assert 0
631669
@@ -678,6 +716,7 @@ here is a little example implemented via a local plugin:
678716
679717
import pytest
680718
719+
681720
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
682721
def pytest_runtest_makereport(item, call):
683722
# execute all other hooks to obtain the report object
@@ -696,10 +735,10 @@ here is a little example implemented via a local plugin:
696735
# request.node is an "item" because we use the default
697736
# "function" scope
698737
if request.node.rep_setup.failed:
699-
print ("setting up a test failed!", request.node.nodeid)
738+
print("setting up a test failed!", request.node.nodeid)
700739
elif request.node.rep_setup.passed:
701740
if request.node.rep_call.failed:
702-
print ("executing test failed", request.node.nodeid)
741+
print("executing test failed", request.node.nodeid)
703742
704743
705744
if you then have failing tests:
@@ -710,16 +749,20 @@ if you then have failing tests:
710749
711750
import pytest
712751
752+
713753
@pytest.fixture
714754
def other():
715755
assert 0
716756
757+
717758
def test_setup_fails(something, other):
718759
pass
719760
761+
720762
def test_call_fails(something):
721763
assert 0
722764
765+
723766
def test_fail2():
724767
assert 0
725768
@@ -787,7 +830,7 @@ test got stuck if necessary:
787830
788831
for pid in psutil.pids():
789832
environ = psutil.Process(pid).environ()
790-
if 'PYTEST_CURRENT_TEST' in environ:
833+
if "PYTEST_CURRENT_TEST" in environ:
791834
print(f'pytest process {pid} running: {environ["PYTEST_CURRENT_TEST"]}')
792835
793836
During the test session pytest will set ``PYTEST_CURRENT_TEST`` to the current test
@@ -841,8 +884,9 @@ like ``pytest-timeout`` they must be imported explicitly and passed on to pytest
841884
import sys
842885
import pytest_timeout # Third party plugin
843886
844-
if len(sys.argv) > 1 and sys.argv[1] == '--pytest':
887+
if len(sys.argv) > 1 and sys.argv[1] == "--pytest":
845888
import pytest
889+
846890
sys.exit(pytest.main(sys.argv[2:], plugins=[pytest_timeout]))
847891
else:
848892
# normal application execution: at this point argv can be parsed

0 commit comments

Comments
 (0)