Skip to content

Commit 76d9e10

Browse files
committed
Refactor tests for warnings
1 parent 1f50050 commit 76d9e10

File tree

2 files changed

+15
-72
lines changed

2 files changed

+15
-72
lines changed

tests/test_warnings.cpp

+9-42
Original file line numberDiff line numberDiff line change
@@ -13,67 +13,34 @@
1313

1414
#include <utility>
1515

16-
namespace warning_helpers {
17-
void warn_function(py::module &m, const char *name, py::handle category, const char *message) {
18-
m.def(name, [category, message]() { py::warnings::warn(message, category); });
19-
}
20-
} // namespace warning_helpers
21-
22-
class CustomWarning {};
23-
2416
TEST_SUBMODULE(warnings_, m) {
2517

2618
// Test warning mechanism base
27-
m.def("raise_and_return", []() {
28-
std::string message = "Warning was raised!";
19+
m.def("warn_and_return_value", []() {
20+
std::string message = "This is simple warning";
2921
py::warnings::warn(message.c_str(), PyExc_Warning);
3022
return 21;
3123
});
3224

33-
m.def("raise_default", []() { py::warnings::warn("RuntimeWarning is raised!"); });
25+
m.def("warn_with_default_category", []() { py::warnings::warn("This is RuntimeWarning"); });
3426

35-
m.def("raise_from_cpython",
36-
[]() { py::warnings::warn("UnicodeWarning is raised!", PyExc_UnicodeWarning); });
27+
m.def("warn_with_different_category",
28+
[]() { py::warnings::warn("This is FutureWarning", PyExc_FutureWarning); });
3729

38-
m.def("raise_and_fail",
39-
[]() { py::warnings::warn("RuntimeError should be raised!", PyExc_Exception); });
30+
m.def("warn_with_invalid_category",
31+
[]() { py::warnings::warn("Invalid category", PyExc_Exception); });
4032

4133
// Test custom warnings
4234
PYBIND11_CONSTINIT static py::gil_safe_call_once_and_store<py::object> ex_storage;
4335
ex_storage.call_once_and_store_result([&]() {
4436
return py::warnings::new_warning_type(m, "CustomWarning", PyExc_DeprecationWarning);
4537
});
4638

47-
m.def("raise_custom", []() {
48-
py::warnings::warn("CustomWarning was raised!", ex_storage.get_stored());
39+
m.def("warn_with_custom_type", []() {
40+
py::warnings::warn("This is CustomWarning", ex_storage.get_stored());
4941
return 37;
5042
});
5143

5244
m.def("register_duplicate_warning",
5345
[m]() { py::warnings::new_warning_type(m, "CustomWarning", PyExc_RuntimeWarning); });
54-
55-
// Bind warning categories
56-
warning_helpers::warn_function(m, "raise_base_warning", PyExc_Warning, "This is Warning!");
57-
warning_helpers::warn_function(
58-
m, "raise_bytes_warning", PyExc_BytesWarning, "This is BytesWarning!");
59-
warning_helpers::warn_function(
60-
m, "raise_deprecation_warning", PyExc_DeprecationWarning, "This is DeprecationWarning!");
61-
warning_helpers::warn_function(
62-
m, "raise_future_warning", PyExc_FutureWarning, "This is FutureWarning!");
63-
warning_helpers::warn_function(
64-
m, "raise_import_warning", PyExc_ImportWarning, "This is ImportWarning!");
65-
warning_helpers::warn_function(m,
66-
"raise_pending_deprecation_warning",
67-
PyExc_PendingDeprecationWarning,
68-
"This is PendingDeprecationWarning!");
69-
warning_helpers::warn_function(
70-
m, "raise_resource_warning", PyExc_ResourceWarning, "This is ResourceWarning!");
71-
warning_helpers::warn_function(
72-
m, "raise_runtime_warning", PyExc_RuntimeWarning, "This is RuntimeWarning!");
73-
warning_helpers::warn_function(
74-
m, "raise_syntax_warning", PyExc_SyntaxWarning, "This is SyntaxWarning!");
75-
warning_helpers::warn_function(
76-
m, "raise_unicode_warning", PyExc_UnicodeWarning, "This is UnicodeWarning!");
77-
warning_helpers::warn_function(
78-
m, "raise_user_warning", PyExc_UserWarning, "This is UserWarning!");
7946
}

tests/test_warnings.py

+6-30
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
@pytest.mark.parametrize(
1212
("expected_category", "expected_message", "expected_value", "module_function"),
1313
[
14-
(Warning, "Warning was raised!", 21, m.raise_and_return),
15-
(RuntimeWarning, "RuntimeWarning is raised!", None, m.raise_default),
16-
(UnicodeWarning, "UnicodeWarning is raised!", None, m.raise_from_cpython),
14+
(Warning, "This is simple warning", 21, m.warn_and_return_value),
15+
(RuntimeWarning, "This is RuntimeWarning", None, m.warn_with_default_category),
16+
(FutureWarning, "This is FutureWarning", None, m.warn_with_different_category),
1717
],
1818
)
1919
def test_warning_simple(
@@ -29,7 +29,7 @@ def test_warning_simple(
2929

3030
def test_warning_wrong_subclass_fail():
3131
with pytest.raises(Exception) as excinfo:
32-
m.raise_and_fail()
32+
m.warn_with_invalid_category()
3333

3434
assert issubclass(excinfo.type, RuntimeError)
3535
assert (
@@ -73,9 +73,9 @@ def test_warning_register():
7373
(
7474
m.CustomWarning,
7575
DeprecationWarning,
76-
"CustomWarning was raised!",
76+
"This is CustomWarning",
7777
37,
78-
m.raise_custom,
78+
m.warn_with_custom_type,
7979
),
8080
],
8181
)
@@ -89,27 +89,3 @@ def test_warning_custom(
8989
assert issubclass(excinfo[0].category, expected_category)
9090
assert str(excinfo[0].message) == expected_message
9191
assert value == expected_value
92-
93-
94-
@pytest.mark.parametrize(
95-
("expected_category", "module_function"),
96-
[
97-
(Warning, m.raise_base_warning),
98-
(BytesWarning, m.raise_bytes_warning),
99-
(DeprecationWarning, m.raise_deprecation_warning),
100-
(FutureWarning, m.raise_future_warning),
101-
(ImportWarning, m.raise_import_warning),
102-
(PendingDeprecationWarning, m.raise_pending_deprecation_warning),
103-
(ResourceWarning, m.raise_resource_warning),
104-
(RuntimeWarning, m.raise_runtime_warning),
105-
(SyntaxWarning, m.raise_syntax_warning),
106-
(UnicodeWarning, m.raise_unicode_warning),
107-
(UserWarning, m.raise_user_warning),
108-
],
109-
)
110-
def test_warning_categories(expected_category, module_function):
111-
with pytest.warns(Warning) as excinfo:
112-
module_function()
113-
114-
assert issubclass(excinfo[0].category, expected_category)
115-
assert str(excinfo[0].message) == f"This is {expected_category.__name__}!"

0 commit comments

Comments
 (0)