Skip to content

Commit 2e74993

Browse files
authored
Make DBTestCase compatible with pytest (#5691)
* Make DbTestCase compatible with pytest * augment note about future enhancement for pytest support * reformat with black
1 parent 7ac88f3 commit 2e74993

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

st2tests/st2tests/base.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
from __future__ import absolute_import
1717
from __future__ import print_function
1818

19+
from unittest.result import TestResult
20+
1921
# NOTE: We need to perform monkeypatch before importing ssl module otherwise tests will fail.
2022
# See https://github.com/StackStorm/st2/pull/4834 for details
2123
from st2common.util.monkey_patch import monkey_patch
@@ -316,7 +318,16 @@ def setUpClass(cls):
316318
def tearDownClass(cls):
317319
drop_db = True
318320

319-
if cls.current_result.errors or cls.current_result.failures:
321+
# TODO: pytst does not make results available to fixtures by default.
322+
# we might be able to add a hook+class fixture to help with this, but
323+
# that adds quite a bit of complexity. For now, pytest will always drop the db.
324+
# https://docs.pytest.org/en/stable/example/simple.html#making-test-result-information-available-in-fixtures
325+
# When someone does decide to tackle this, we will probably need to rename the db
326+
# for later inspection so subsequent tests still have a clean starting point as
327+
# pytest will not necessarily stop on failure like nosetest did.
328+
if cls.current_result and (
329+
cls.current_result.errors or cls.current_result.failures
330+
):
320331
# Don't drop DB on test failure
321332
drop_db = False
322333

@@ -325,8 +336,11 @@ def tearDownClass(cls):
325336

326337
def run(self, result=None):
327338
# Remember result for use in tearDown and tearDownClass
328-
self.current_result = result
329-
self.__class__.current_result = result
339+
# pytest sets result to _pytest.unittest.TestCaseFunction
340+
# which does not have attributes: errors, failures
341+
if isinstance(result, TestResult):
342+
self.current_result = result
343+
self.__class__.current_result = result
330344
super(DbTestCase, self).run(result=result)
331345

332346

0 commit comments

Comments
 (0)