Skip to content

Commit

Permalink
Merge pull request #5096 from asottile/docs_highlight
Browse files Browse the repository at this point in the history
 blacken-docs more code samples in docs
  • Loading branch information
nicoddemus authored Apr 12, 2019
2 parents 1dafe96 + da2e092 commit a9e850f
Show file tree
Hide file tree
Showing 13 changed files with 305 additions and 131 deletions.
14 changes: 7 additions & 7 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
exclude: doc/en/example/py2py3/test_py2.py
repos:
- repo: https://github.com/ambv/black
rev: 18.9b0
rev: 19.3b0
hooks:
- id: black
args: [--safe, --quiet]
language_version: python3
- repo: https://github.com/asottile/blacken-docs
rev: v0.3.0
rev: v0.5.0
hooks:
- id: blacken-docs
additional_dependencies: [black==18.9b0]
additional_dependencies: [black==19.3b0]
language_version: python3
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.1.0
Expand All @@ -22,22 +22,22 @@ repos:
exclude: _pytest/debugging.py
language_version: python3
- repo: https://gitlab.com/pycqa/flake8
rev: 3.7.0
rev: 3.7.7
hooks:
- id: flake8
language_version: python3
- repo: https://github.com/asottile/reorder_python_imports
rev: v1.3.5
rev: v1.4.0
hooks:
- id: reorder-python-imports
args: ['--application-directories=.:src']
- repo: https://github.com/asottile/pyupgrade
rev: v1.11.1
rev: v1.15.0
hooks:
- id: pyupgrade
args: [--keep-percent-format]
- repo: https://github.com/pre-commit/pygrep-hooks
rev: v1.2.0
rev: v1.3.0
hooks:
- id: rst-backticks
- repo: local
Expand Down
2 changes: 1 addition & 1 deletion bench/bench_argcomplete.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@

if __name__ == "__main__":
print(timeit.timeit(run, setup=setup % imports[0], number=count))
print((timeit.timeit(run, setup=setup % imports[1], number=count)))
print(timeit.timeit(run, setup=setup % imports[1], number=count))
57 changes: 43 additions & 14 deletions doc/en/assert.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ Asserting with the ``assert`` statement

``pytest`` allows you to use the standard python ``assert`` for verifying
expectations and values in Python tests. For example, you can write the
following::
following:

.. code-block:: python
# content of test_assert1.py
def f():
return 3
def test_function():
assert f() == 4
Expand Down Expand Up @@ -52,7 +55,9 @@ operators. (See :ref:`tbreportdemo`). This allows you to use the
idiomatic python constructs without boilerplate code while not losing
introspection information.

However, if you specify a message with the assertion like this::
However, if you specify a message with the assertion like this:

.. code-block:: python
assert a % 2 == 0, "value was odd, should be even"
Expand All @@ -67,38 +72,49 @@ Assertions about expected exceptions
------------------------------------------

In order to write assertions about raised exceptions, you can use
``pytest.raises`` as a context manager like this::
``pytest.raises`` as a context manager like this:

.. code-block:: python
import pytest
def test_zero_division():
with pytest.raises(ZeroDivisionError):
1 / 0
and if you need to have access to the actual exception info you may use::
and if you need to have access to the actual exception info you may use:

.. code-block:: python
def test_recursion_depth():
with pytest.raises(RuntimeError) as excinfo:
def f():
f()
f()
assert 'maximum recursion' in str(excinfo.value)
assert "maximum recursion" in str(excinfo.value)
``excinfo`` is a ``ExceptionInfo`` instance, which is a wrapper around
the actual exception raised. The main attributes of interest are
``.type``, ``.value`` and ``.traceback``.

You can pass a ``match`` keyword parameter to the context-manager to test
that a regular expression matches on the string representation of an exception
(similar to the ``TestCase.assertRaisesRegexp`` method from ``unittest``)::
(similar to the ``TestCase.assertRaisesRegexp`` method from ``unittest``):

.. code-block:: python
import pytest
def myfunc():
raise ValueError("Exception 123 raised")
def test_match():
with pytest.raises(ValueError, match=r'.* 123 .*'):
with pytest.raises(ValueError, match=r".* 123 .*"):
myfunc()
The regexp parameter of the ``match`` method is matched with the ``re.search``
Expand All @@ -107,7 +123,9 @@ well.

There's an alternate form of the ``pytest.raises`` function where you pass
a function that will be executed with the given ``*args`` and ``**kwargs`` and
assert that the given exception is raised::
assert that the given exception is raised:

.. code-block:: python
pytest.raises(ExpectedException, func, *args, **kwargs)
Expand All @@ -116,7 +134,9 @@ exception* or *wrong exception*.

Note that it is also possible to specify a "raises" argument to
``pytest.mark.xfail``, which checks that the test is failing in a more
specific way than just having any exception raised::
specific way than just having any exception raised:

.. code-block:: python
@pytest.mark.xfail(raises=IndexError)
def test_f():
Expand Down Expand Up @@ -148,10 +168,13 @@ Making use of context-sensitive comparisons
.. versionadded:: 2.0

``pytest`` has rich support for providing context-sensitive information
when it encounters comparisons. For example::
when it encounters comparisons. For example:

.. code-block:: python
# content of test_assert2.py
def test_set_comparison():
set1 = set("1308")
set2 = set("8035")
Expand Down Expand Up @@ -205,16 +228,21 @@ the ``pytest_assertrepr_compare`` hook.
:noindex:

As an example consider adding the following hook in a :ref:`conftest.py <conftest.py>`
file which provides an alternative explanation for ``Foo`` objects::
file which provides an alternative explanation for ``Foo`` objects:

.. code-block:: python
# content of conftest.py
from test_foocompare import Foo
def pytest_assertrepr_compare(op, left, right):
if isinstance(left, Foo) and isinstance(right, Foo) and op == "==":
return ['Comparing Foo instances:',
' vals: %s != %s' % (left.val, right.val)]
return ["Comparing Foo instances:", " vals: %s != %s" % (left.val, right.val)]
now, given this test module::
now, given this test module:

.. code-block:: python
# content of test_foocompare.py
class Foo(object):
Expand All @@ -224,6 +252,7 @@ now, given this test module::
def __eq__(self, other):
return self.val == other.val
def test_compare():
f1 = Foo(1)
f2 = Foo(2)
Expand Down
Loading

0 comments on commit a9e850f

Please sign in to comment.