Skip to content

Commit 340cea2

Browse files
committed
Add unittests for strtobool and parse_warning_filter
1 parent 8e82b40 commit 340cea2

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

testing/test_config.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,22 @@
55
from typing import Dict
66
from typing import List
77
from typing import Sequence
8+
from typing import Tuple
89

910
import attr
1011
import py.path
1112

1213
import _pytest._code
1314
import pytest
1415
from _pytest.compat import importlib_metadata
16+
from _pytest.compat import TYPE_CHECKING
1517
from _pytest.config import _get_plugin_specs_as_list
1618
from _pytest.config import _iter_rewritable_modules
19+
from _pytest.config import _strtobool
1720
from _pytest.config import Config
1821
from _pytest.config import ConftestImportFailure
1922
from _pytest.config import ExitCode
23+
from _pytest.config import parse_warning_filter
2024
from _pytest.config.exceptions import UsageError
2125
from _pytest.config.findpaths import determine_setup
2226
from _pytest.config.findpaths import get_common_ancestor
@@ -25,6 +29,9 @@
2529
from _pytest.pathlib import Path
2630
from _pytest.pytester import Testdir
2731

32+
if TYPE_CHECKING:
33+
from typing import Type
34+
2835

2936
class TestParseIni:
3037
@pytest.mark.parametrize(
@@ -1829,3 +1836,52 @@ def test_conftest_import_error_repr(tmpdir):
18291836
assert exc.__traceback__ is not None
18301837
exc_info = (type(exc), exc, exc.__traceback__)
18311838
raise ConftestImportFailure(path, exc_info) from exc
1839+
1840+
1841+
def test_strtobooL():
1842+
assert _strtobool("YES")
1843+
assert not _strtobool("NO")
1844+
with pytest.raises(ValueError):
1845+
_strtobool("unknown")
1846+
1847+
1848+
@pytest.mark.parametrize(
1849+
"arg, escape, expected",
1850+
[
1851+
("ignore", False, ("ignore", "", Warning, "", 0)),
1852+
(
1853+
"ignore::DeprecationWarning",
1854+
False,
1855+
("ignore", "", DeprecationWarning, "", 0),
1856+
),
1857+
(
1858+
"ignore:some msg:DeprecationWarning",
1859+
False,
1860+
("ignore", "some msg", DeprecationWarning, "", 0),
1861+
),
1862+
(
1863+
"ignore::DeprecationWarning:mod",
1864+
False,
1865+
("ignore", "", DeprecationWarning, "mod", 0),
1866+
),
1867+
(
1868+
"ignore::DeprecationWarning:mod:42",
1869+
False,
1870+
("ignore", "", DeprecationWarning, "mod", 42),
1871+
),
1872+
("error:some\\msg:::", True, ("error", "some\\\\msg", Warning, "", 0)),
1873+
("error:::mod\\foo:", True, ("error", "", Warning, "mod\\\\foo\\Z", 0)),
1874+
],
1875+
)
1876+
def test_parse_warning_filter(
1877+
arg: str, escape: bool, expected: "Tuple[str, str, Type[Warning], str, int]"
1878+
) -> None:
1879+
assert parse_warning_filter(arg, escape=escape) == expected
1880+
1881+
1882+
@pytest.mark.parametrize("arg", [":" * 5, "::::-1", "::::not-a-number"])
1883+
def test_parse_warning_filter_failure(arg: str) -> None:
1884+
import warnings
1885+
1886+
with pytest.raises(warnings._OptionError):
1887+
parse_warning_filter(arg, escape=True)

0 commit comments

Comments
 (0)