@@ -497,79 +497,6 @@ The :mod:`test.support` module defines the following functions:
497
497
check_impl_detail(cpython=False) # Everywhere except CPython.
498
498
499
499
500
- .. function :: check_warnings(\*filters, quiet=True)
501
-
502
- A convenience wrapper for :func: `warnings.catch_warnings() ` that makes it
503
- easier to test that a warning was correctly raised. It is approximately
504
- equivalent to calling ``warnings.catch_warnings(record=True) `` with
505
- :meth: `warnings.simplefilter ` set to ``always `` and with the option to
506
- automatically validate the results that are recorded.
507
-
508
- ``check_warnings `` accepts 2-tuples of the form ``("message regexp",
509
- WarningCategory) `` as positional arguments. If one or more *filters * are
510
- provided, or if the optional keyword argument *quiet * is ``False ``,
511
- it checks to make sure the warnings are as expected: each specified filter
512
- must match at least one of the warnings raised by the enclosed code or the
513
- test fails, and if any warnings are raised that do not match any of the
514
- specified filters the test fails. To disable the first of these checks,
515
- set *quiet * to ``True ``.
516
-
517
- If no arguments are specified, it defaults to::
518
-
519
- check_warnings(("", Warning), quiet=True)
520
-
521
- In this case all warnings are caught and no errors are raised.
522
-
523
- On entry to the context manager, a :class: `WarningRecorder ` instance is
524
- returned. The underlying warnings list from
525
- :func: `~warnings.catch_warnings ` is available via the recorder object's
526
- :attr: `warnings ` attribute. As a convenience, the attributes of the object
527
- representing the most recent warning can also be accessed directly through
528
- the recorder object (see example below). If no warning has been raised,
529
- then any of the attributes that would otherwise be expected on an object
530
- representing a warning will return ``None ``.
531
-
532
- The recorder object also has a :meth: `reset ` method, which clears the
533
- warnings list.
534
-
535
- The context manager is designed to be used like this::
536
-
537
- with check_warnings(("assertion is always true", SyntaxWarning),
538
- ("", UserWarning)):
539
- exec('assert(False, "Hey!")')
540
- warnings.warn(UserWarning("Hide me!"))
541
-
542
- In this case if either warning was not raised, or some other warning was
543
- raised, :func: `check_warnings ` would raise an error.
544
-
545
- When a test needs to look more deeply into the warnings, rather than
546
- just checking whether or not they occurred, code like this can be used::
547
-
548
- with check_warnings(quiet=True) as w:
549
- warnings.warn("foo")
550
- assert str(w.args[0]) == "foo"
551
- warnings.warn("bar")
552
- assert str(w.args[0]) == "bar"
553
- assert str(w.warnings[0].args[0]) == "foo"
554
- assert str(w.warnings[1].args[0]) == "bar"
555
- w.reset()
556
- assert len(w.warnings) == 0
557
-
558
-
559
- Here all warnings will be caught, and the test code tests the captured
560
- warnings directly.
561
-
562
- .. versionchanged :: 3.2
563
- New optional arguments *filters * and *quiet *.
564
-
565
-
566
- .. function :: check_no_resource_warning(testcase)
567
-
568
- Context manager to check that no :exc: `ResourceWarning ` was raised. You
569
- must remove the object which may emit :exc: `ResourceWarning ` before the
570
- end of the context manager.
571
-
572
-
573
500
.. function :: set_memlimit(limit)
574
501
575
502
Set the values for :data: `max_memuse ` and :data: `real_max_memuse ` for big
@@ -851,20 +778,6 @@ The :mod:`test.support` module defines the following functions:
851
778
the offset of the exception.
852
779
853
780
854
- .. function :: check_syntax_warning(testcase, statement, errtext='', *, lineno=1, offset=None)
855
-
856
- Test for syntax warning in *statement * by attempting to compile *statement *.
857
- Test also that the :exc: `SyntaxWarning ` is emitted only once, and that it
858
- will be converted to a :exc: `SyntaxError ` when turned into error.
859
- *testcase * is the :mod: `unittest ` instance for the test. *errtext * is the
860
- regular expression which should match the string representation of the
861
- emitted :exc: `SyntaxWarning ` and raised :exc: `SyntaxError `. If *lineno *
862
- is not ``None ``, compares to the line of the warning and exception.
863
- If *offset * is not ``None ``, compares to the offset of the exception.
864
-
865
- .. versionadded :: 3.8
866
-
867
-
868
781
.. function :: open_urlresource(url, *args, **kw)
869
782
870
783
Open *url *. If open fails, raises :exc: `TestFailed `.
@@ -1051,12 +964,6 @@ The :mod:`test.support` module defines the following classes:
1051
964
Try to match a single stored value (*dv *) with a supplied value (*v *).
1052
965
1053
966
1054
- .. class :: WarningsRecorder()
1055
-
1056
- Class used to record warnings for unit tests. See documentation of
1057
- :func: `check_warnings ` above for more details.
1058
-
1059
-
1060
967
.. class :: BasicTestRunner()
1061
968
1062
969
.. method :: run(test)
@@ -1659,3 +1566,105 @@ The :mod:`test.support.import_helper` module provides support for import tests.
1659
1566
will be reverted at the end of the block.
1660
1567
1661
1568
1569
+ :mod: `test.support.warnings_helper ` --- Utilities for warnings tests
1570
+ ====================================================================
1571
+
1572
+ .. module :: test.support.warnings_helper
1573
+ :synopsis: Support for warnings tests.
1574
+
1575
+ The :mod: `test.support.warnings_helper ` module provides support for warnings tests.
1576
+
1577
+ .. versionadded :: 3.10
1578
+
1579
+
1580
+ .. function :: check_no_resource_warning(testcase)
1581
+
1582
+ Context manager to check that no :exc: `ResourceWarning ` was raised. You
1583
+ must remove the object which may emit :exc: `ResourceWarning ` before the
1584
+ end of the context manager.
1585
+
1586
+
1587
+ .. function :: check_syntax_warning(testcase, statement, errtext='', *, lineno=1, offset=None)
1588
+
1589
+ Test for syntax warning in *statement * by attempting to compile *statement *.
1590
+ Test also that the :exc: `SyntaxWarning ` is emitted only once, and that it
1591
+ will be converted to a :exc: `SyntaxError ` when turned into error.
1592
+ *testcase * is the :mod: `unittest ` instance for the test. *errtext * is the
1593
+ regular expression which should match the string representation of the
1594
+ emitted :exc: `SyntaxWarning ` and raised :exc: `SyntaxError `. If *lineno *
1595
+ is not ``None ``, compares to the line of the warning and exception.
1596
+ If *offset * is not ``None ``, compares to the offset of the exception.
1597
+
1598
+ .. versionadded :: 3.8
1599
+
1600
+
1601
+ .. function :: check_warnings(\*filters, quiet=True)
1602
+
1603
+ A convenience wrapper for :func: `warnings.catch_warnings() ` that makes it
1604
+ easier to test that a warning was correctly raised. It is approximately
1605
+ equivalent to calling ``warnings.catch_warnings(record=True) `` with
1606
+ :meth: `warnings.simplefilter ` set to ``always `` and with the option to
1607
+ automatically validate the results that are recorded.
1608
+
1609
+ ``check_warnings `` accepts 2-tuples of the form ``("message regexp",
1610
+ WarningCategory) `` as positional arguments. If one or more *filters * are
1611
+ provided, or if the optional keyword argument *quiet * is ``False ``,
1612
+ it checks to make sure the warnings are as expected: each specified filter
1613
+ must match at least one of the warnings raised by the enclosed code or the
1614
+ test fails, and if any warnings are raised that do not match any of the
1615
+ specified filters the test fails. To disable the first of these checks,
1616
+ set *quiet * to ``True ``.
1617
+
1618
+ If no arguments are specified, it defaults to::
1619
+
1620
+ check_warnings(("", Warning), quiet=True)
1621
+
1622
+ In this case all warnings are caught and no errors are raised.
1623
+
1624
+ On entry to the context manager, a :class: `WarningRecorder ` instance is
1625
+ returned. The underlying warnings list from
1626
+ :func: `~warnings.catch_warnings ` is available via the recorder object's
1627
+ :attr: `warnings ` attribute. As a convenience, the attributes of the object
1628
+ representing the most recent warning can also be accessed directly through
1629
+ the recorder object (see example below). If no warning has been raised,
1630
+ then any of the attributes that would otherwise be expected on an object
1631
+ representing a warning will return ``None ``.
1632
+
1633
+ The recorder object also has a :meth: `reset ` method, which clears the
1634
+ warnings list.
1635
+
1636
+ The context manager is designed to be used like this::
1637
+
1638
+ with check_warnings(("assertion is always true", SyntaxWarning),
1639
+ ("", UserWarning)):
1640
+ exec('assert(False, "Hey!")')
1641
+ warnings.warn(UserWarning("Hide me!"))
1642
+
1643
+ In this case if either warning was not raised, or some other warning was
1644
+ raised, :func: `check_warnings ` would raise an error.
1645
+
1646
+ When a test needs to look more deeply into the warnings, rather than
1647
+ just checking whether or not they occurred, code like this can be used::
1648
+
1649
+ with check_warnings(quiet=True) as w:
1650
+ warnings.warn("foo")
1651
+ assert str(w.args[0]) == "foo"
1652
+ warnings.warn("bar")
1653
+ assert str(w.args[0]) == "bar"
1654
+ assert str(w.warnings[0].args[0]) == "foo"
1655
+ assert str(w.warnings[1].args[0]) == "bar"
1656
+ w.reset()
1657
+ assert len(w.warnings) == 0
1658
+
1659
+
1660
+ Here all warnings will be caught, and the test code tests the captured
1661
+ warnings directly.
1662
+
1663
+ .. versionchanged :: 3.2
1664
+ New optional arguments *filters * and *quiet *.
1665
+
1666
+
1667
+ .. class :: WarningsRecorder()
1668
+
1669
+ Class used to record warnings for unit tests. See documentation of
1670
+ :func: `check_warnings ` above for more details.
0 commit comments