Skip to content

Commit ffdedcc

Browse files
committed
[FIX] support subTest context manager for Odoo version 16.0 and greater
1 parent 0eb0398 commit ffdedcc

File tree

4 files changed

+74
-19
lines changed

4 files changed

+74
-19
lines changed

pytest_odoo.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import subprocess
1111
import threading
1212
from contextlib import contextmanager
13-
from unittest import mock, TestCase
13+
from unittest import mock, TestCase as UnitTestTestCase
1414
from pathlib import Path
1515
from typing import Optional
1616

@@ -217,18 +217,19 @@ def disable_odoo_test_retry():
217217
pass
218218

219219
def support_subtest():
220-
"""Odoo from version 16.0 overwrite TestCase.SubTest context manager
220+
"""Odoo from version 16.0 re-define its own TestCase.subTest context manager
221221
222-
This overwrite assume the usage of OdooTestResult which we are not
223-
using with pytest-odoo. So this restaure unitest SubTest Context manager
222+
Odoo assume the usage of OdooTestResult which is not our case
223+
using with pytest-odoo. So this fallback to the unitest.TestCase.subTest
224+
Context manager
224225
"""
225226
try:
226-
from odoo.tests import BaseCase
227-
BaseCase.subTest = TestCase.subTest
227+
from odoo.tests.case import TestCase
228+
TestCase.subTest = UnitTestTestCase.subTest
228229

229230
from odoo.tests.case import _Outcome
230231
_Outcome.result_supports_subtests = False
231-
except (ImportError, AttributeError):
232+
except ImportError:
232233
# Odoo <= 15.0
233234
pass
234235

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from unittest.mock import MagicMock
22
common = MagicMock()
3+
from . import case
34

4-
5-
class BaseCase:
5+
class BaseCase(case.TestCase):
66

77
def run(*args, **kwargs):
88
super().run(*args, **kwargs)

tests/mock/odoo/odoo/tests/case.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import contextlib
2+
3+
4+
class TestCase:
5+
6+
@contextlib.contextmanager
7+
def subTest(self, **kwargs):
8+
"""Simulate odoo TestCase.subTest from version 15.0"""
9+
10+
11+
class _Outcome:
12+
pass

tests/test_pytest_odoo.py

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
from _pytest import pathlib as pytest_pathlib
77
from pytest_odoo import (
88
_find_manifest_path,
9-
monkey_patch_resolve_pkg_root_and_module_name,
109
disable_odoo_test_retry,
10+
monkey_patch_resolve_pkg_root_and_module_name,
11+
support_subtest,
1112
)
1213

1314

@@ -93,7 +94,7 @@ def test_disable_odoo_test_retry(self):
9394

9495
def restore_basecase_run():
9596
BaseCase.run = original_basecase_run
96-
97+
9798
self.addCleanup(restore_basecase_run)
9899

99100
disable_odoo_test_retry()
@@ -107,26 +108,67 @@ def test_disable_odoo_test_retry_ignore_run_doesnt_exists(self):
107108

108109
def restore_basecase_run():
109110
BaseCase.run = original_basecase_run
110-
111+
111112
self.addCleanup(restore_basecase_run)
112-
113+
113114
del BaseCase.run
114-
115+
115116
disable_odoo_test_retry()
116117
self.assertFalse(hasattr(BaseCase, "run"))
117118

118119

119120

120121
def test_import_error(self):
121-
from odoo import tests
122-
122+
from odoo import tests
123+
123124
original_BaseCase = tests.BaseCase
124125

125126
def restore_basecase():
126127
tests.BaseCase = original_BaseCase
127-
128+
128129
self.addCleanup(restore_basecase)
129-
130+
130131
del tests.BaseCase
131132
disable_odoo_test_retry()
132-
133+
134+
def test_support_subtest(self):
135+
from odoo.tests import case
136+
137+
original_test_case = case.TestCase
138+
original_outcome = case._Outcome
139+
140+
def restore():
141+
case.TestCase = original_test_case
142+
case._Outcome = original_outcome
143+
144+
self.addCleanup(restore)
145+
support_subtest()
146+
from odoo.tests import BaseCase
147+
148+
self.assertTrue(BaseCase.subTest is TestCase.subTest)
149+
150+
def test_support_subtest_no_base_case(self):
151+
from odoo import tests
152+
153+
original_BaseCase = tests.BaseCase
154+
155+
def restore_basecase():
156+
tests.BaseCase = original_BaseCase
157+
158+
self.addCleanup(restore_basecase)
159+
160+
del tests.BaseCase
161+
support_subtest()
162+
163+
def test_support_subtest_import_error(self):
164+
from odoo.tests import case
165+
166+
original_odoo_test_case = case.TestCase
167+
168+
def restore_testcase():
169+
case.TestCase = original_odoo_test_case
170+
171+
self.addCleanup(restore_testcase)
172+
173+
del case.TestCase
174+
support_subtest()

0 commit comments

Comments
 (0)