Skip to content

Commit 43190db

Browse files
authored
Add test name to window caption when running tests (#2386)
* Add make_window_caption helper in conftest.py * Add test_name fixture for use in function-scoped fixtures * Add caption argument to prepare_window in conftest.py * Use test_name fixture in function-scoped fixtures * Inline the test name fetching for static context (session-scoped) fixtures
1 parent 85eb020 commit 43190db

File tree

1 file changed

+34
-9
lines changed

1 file changed

+34
-9
lines changed

tests/conftest.py

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,21 @@
2626
OFFSCREEN = None
2727

2828

29+
def make_window_caption(request=None, prefix='Testing', sep=' - ') -> str:
30+
"""Centralizes test name customization.
31+
32+
It helps with:
33+
34+
1. Tests scoped as something other than function (can't use test_name fixture)
35+
2. Local (non-CI) temp modifications of inter-test behavior
36+
"""
37+
parts = [prefix]
38+
if request is not None:
39+
parts.append(request.node.name)
40+
41+
return sep.join(parts)
42+
43+
2944
def create_window(width=1280, height=720, caption="Testing", **kwargs):
3045
global WINDOW
3146
if not WINDOW:
@@ -39,14 +54,16 @@ def create_window(width=1280, height=720, caption="Testing", **kwargs):
3954
return WINDOW
4055

4156

42-
def prepare_window(window: arcade.Window):
57+
def prepare_window(window: arcade.Window, caption: str | None = None):
4358
# Check if someone has been naughty
4459
if window.has_exit:
4560
raise RuntimeError("Please do not close the global test window :D")
4661

4762
window.switch_to()
4863
if window.get_size() < (800, 600):
4964
window.set_size(800, 600)
65+
if caption:
66+
window.set_caption(caption)
5067

5168
ctx = window.ctx
5269
# ctx._atlas = None # Clear the global atlas
@@ -75,8 +92,13 @@ def prepare_window(window: arcade.Window):
7592
window.on_update = lambda dt: None
7693

7794

95+
@pytest.fixture
96+
def test_name(request):
97+
return make_window_caption(request)
98+
99+
78100
@pytest.fixture(scope="function")
79-
def ctx():
101+
def ctx(test_name):
80102
"""
81103
Per function context.
82104
@@ -85,25 +107,28 @@ def ctx():
85107
"""
86108
window = create_window()
87109
arcade.set_window(window)
88-
prepare_window(window)
110+
prepare_window(window, caption=test_name)
89111
return window.ctx
90112

91113

92114
@pytest.fixture(scope="session")
93-
def ctx_static():
115+
def ctx_static(request):
94116
"""
95117
Context that is shared between tests
96118
This is the same global context.
97119
Module scoped fixtures can inject this context.
98120
"""
99121
window = create_window()
100122
arcade.set_window(window)
101-
prepare_window(window)
123+
# Can't use the test_name fixture here:
124+
# 1. This fixture is session scoped
125+
# 2. test_name is function scoped
126+
prepare_window(window, caption=make_window_caption(request))
102127
return window.ctx
103128

104129

105130
@pytest.fixture(scope="function")
106-
def window():
131+
def window(test_name):
107132
"""
108133
Global window that is shared between tests.
109134
@@ -113,7 +138,7 @@ def window():
113138
"""
114139
window = create_window()
115140
arcade.set_window(window)
116-
prepare_window(window)
141+
prepare_window(window, caption=test_name)
117142
return window
118143

119144

@@ -401,7 +426,7 @@ def assert_images_almost_equal(self, image1: PIL.Image.Image, image2: PIL.Image.
401426

402427

403428
@pytest.fixture(scope="function")
404-
def offscreen():
429+
def offscreen(test_name):
405430
"""
406431
Offscreen rendering tools.
407432
@@ -411,7 +436,7 @@ def offscreen():
411436

412437
window = create_window()
413438
arcade.set_window(window)
414-
prepare_window(window)
439+
prepare_window(window, caption=test_name)
415440

416441
if OFFSCREEN is None:
417442
OFFSCREEN = Offscreen()

0 commit comments

Comments
 (0)