Skip to content

Commit 6dd6edb

Browse files
committed
test: add subtest support to testTokenlessCsrfProtection
Make each list driven test case a subtest of its own. Add pytest-subtest pip module to make this work.
1 parent a11a4aa commit 6dd6edb

3 files changed

Lines changed: 35 additions & 6 deletions

File tree

.github/workflows/ci-test.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,10 @@ jobs:
147147
run: python -m pip install --upgrade pip
148148

149149
# note pytest-env is not needed for redis password as there is
150-
# no password on CI's redis.
150+
# no password on CI's redis. pytest-subtests isn't needed with
151+
# pytest 9.0.0+, but 9.0 only supports 3.10+ so include for 3.7+.
151152
- name: Install pytest and other packages needed for running tests
152-
run: pip install flake8 hypothesis mock pytest pytest-cov requests sphinx-tabs
153+
run: pip install flake8 hypothesis mock pytest pytest-cov pytest-subtests requests sphinx-tabs
153154

154155
# https://github.com/mxschmitt/action-tmate
155156
# allow remote ssh into the CI container. I need this to debug

test/requirements.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
# the driving harness
22
pytest
33

4+
# set environment variables for redis password etc. from ini/toml
5+
# settings files.
6+
pytest-env
7+
8+
# change order of tests to find dependencies
9+
pytest-randomly
10+
11+
# allows execution of subtests when a test function tests multiple
12+
# scenarios. It is built into pytest 9.0+, but can co-exist with it
13+
# and is required for older pytest that run on older python versions.
14+
pytest-subtests
15+
416
# coverage stats
517
coverage
618
# fuzz tests

test/test_cgi.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,22 @@ def setUp(self):
219219
re.VERBOSE)
220220

221221
@pytest.fixture(autouse=True)
222-
def inject_fixtures(self, caplog, monkeypatch):
222+
def inject_fixtures(self, caplog, monkeypatch, subtests):
223+
"""Used to add pytest test fixtures to unittest based tests
224+
because adding the name of a fixture to a method does not
225+
make the method available.
226+
227+
So the decrorator runs with autouse=True to set params on
228+
the unittest "self" making the fixtures available for
229+
tests.
230+
231+
If you need another fixture, just add it to the argument list
232+
and assign it to self and pytest will make it available.
233+
"""
234+
223235
self._caplog = caplog
224236
self._monkeypatch = monkeypatch
237+
self._subtests = subtests
225238

226239
#
227240
# form label extraction
@@ -1207,10 +1220,13 @@ def hasPermission(s, p, classname=None, d=None, e=None, **kw):
12071220
cl.db.config['WEB_ALLOWED_API_ORIGINS'] = ""
12081221

12091222
cl.main()
1210-
self.assertIn(test['Result'], out[0])
12111223

1212-
if "response_code" in test:
1213-
self.assertEqual(test['response_code'], cl.response_code)
1224+
# make each case above a subtest
1225+
with self._subtests.test(entry=entry):
1226+
self.assertIn(test['Result'], out[0])
1227+
1228+
if "response_code" in test:
1229+
self.assertEqual(test['response_code'], cl.response_code)
12141230

12151231
del(out[0])
12161232

0 commit comments

Comments
 (0)