|
10 | 10 |
|
11 | 11 | import math
|
12 | 12 | import sys
|
| 13 | +from collections.abc import Sequence |
13 | 14 | from contextlib import contextmanager
|
14 | 15 | from random import Random
|
15 |
| -from typing import Optional, Sequence |
| 16 | +from typing import Optional |
16 | 17 |
|
17 | 18 | import pytest
|
18 | 19 |
|
|
31 | 32 | from hypothesis.internal.intervalsets import IntervalSet
|
32 | 33 |
|
33 | 34 | from tests.common.debug import minimal
|
| 35 | +from tests.common.utils import capture_observations |
34 | 36 | from tests.conjecture.common import ir_nodes
|
35 | 37 |
|
36 | 38 |
|
@@ -358,7 +360,7 @@ def test_function(n):
|
358 | 360 |
|
359 | 361 |
|
360 | 362 | def test_flaky_with_backend():
|
361 |
| - with temp_register_backend("trivial", TrivialProvider): |
| 363 | + with temp_register_backend("trivial", TrivialProvider), capture_observations(): |
362 | 364 |
|
363 | 365 | calls = 0
|
364 | 366 |
|
@@ -428,3 +430,42 @@ def test_function(data):
|
428 | 430 | assert n1 <= n2
|
429 | 431 |
|
430 | 432 | test_function()
|
| 433 | + |
| 434 | + |
| 435 | +class ObservableProvider(TrivialProvider): |
| 436 | + def observe_test_case(self): |
| 437 | + return {"msg_key": "some message", "data_key": [1, "2", {}]} |
| 438 | + |
| 439 | + def observe_information_messages(self, *, lifetime): |
| 440 | + if lifetime == "test_case": |
| 441 | + yield {"type": "info", "title": "trivial-data", "content": {"k2": "v2"}} |
| 442 | + else: |
| 443 | + assert lifetime == "test_function" |
| 444 | + yield {"type": "alert", "title": "Trivial alert", "content": "message here"} |
| 445 | + yield {"type": "info", "title": "trivial-data", "content": {"k2": "v2"}} |
| 446 | + |
| 447 | + |
| 448 | +def test_custom_observations_from_backend(): |
| 449 | + with ( |
| 450 | + temp_register_backend("observable", ObservableProvider), |
| 451 | + capture_observations() as ls, |
| 452 | + ): |
| 453 | + |
| 454 | + @given(st.none()) |
| 455 | + @settings(backend="observable", database=None) |
| 456 | + def test_function(_): |
| 457 | + pass |
| 458 | + |
| 459 | + test_function() |
| 460 | + |
| 461 | + assert len(ls) >= 3 |
| 462 | + cases = [t["metadata"]["backend"] for t in ls if t["type"] == "test_case"] |
| 463 | + assert {"msg_key": "some message", "data_key": [1, "2", {}]} in cases |
| 464 | + |
| 465 | + infos = [ |
| 466 | + {k: v for k, v in t.items() if k in ("title", "content")} |
| 467 | + for t in ls |
| 468 | + if t["type"] != "test_case" |
| 469 | + ] |
| 470 | + assert {"title": "Trivial alert", "content": "message here"} in infos |
| 471 | + assert {"title": "trivial-data", "content": {"k2": "v2"}} in infos |
0 commit comments