MCPcopy Create free account
hub / github.com/pytest-dev/pytest / test_stash

Function test_stash

testing/test_stash.py:6–67  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

4
5
6def test_stash() -> None:
7 stash = Stash()
8
9 assert len(stash) == 0
10 assert not stash
11
12 key1 = StashKey[str]()
13 key2 = StashKey[int]()
14
15 # Basic functionality - single key.
16 assert key1 not in stash
17 stash[key1] = "hello"
18 assert key1 in stash
19 assert stash[key1] == "hello"
20 assert stash.get(key1, None) == "hello"
21 stash[key1] = "world"
22 assert stash[key1] == "world"
23 # Has correct type (no mypy error).
24 stash[key1] + "string"
25 assert len(stash) == 1
26 assert stash
27
28 # No interaction with another key.
29 assert key2 not in stash
30 assert stash.get(key2, None) is None
31 with pytest.raises(KeyError):
32 stash[key2]
33 with pytest.raises(KeyError):
34 del stash[key2]
35 stash[key2] = 1
36 assert stash[key2] == 1
37 # Has correct type (no mypy error).
38 stash[key2] + 20
39 del stash[key1]
40 with pytest.raises(KeyError):
41 del stash[key1]
42 with pytest.raises(KeyError):
43 stash[key1]
44
45 # setdefault
46 stash[key1] = "existing"
47 assert stash.setdefault(key1, "default") == "existing"
48 assert stash[key1] == "existing"
49 key_setdefault = StashKey[bytes]()
50 assert stash.setdefault(key_setdefault, b"default") == b"default"
51 assert stash[key_setdefault] == b"default"
52 assert len(stash) == 3
53 assert stash
54
55 # Can't accidentally add attributes to stash object itself.
56 with pytest.raises(AttributeError):
57 stash.foo = "nope" # type: ignore[attr-defined]
58
59 # No interaction with another stash.
60 stash2 = Stash()
61 key3 = StashKey[int]()
62 assert key2 not in stash2
63 stash2[key2] = 100

Callers

nothing calls this directly

Calls 3

getMethod · 0.95
setdefaultMethod · 0.95
StashClass · 0.90

Tested by

no test coverage detected