Skip to content

Fix support to subTest context manager for odoo >= 16.0 #86

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

petrus-v
Copy link
Contributor

@petrus-v petrus-v commented Jul 10, 2025

  • fix to avoid hiding proper error
  • make it working using pytest-subtests
  • support to python 3.13

I've test this PR against on real project using Odoo 14.0 and 17.0

To specifically test this feature :

  • add some tests using the subTest context manager
from odoo.tests.common import TransactionCase


class AnExampleCase(TransactionCase):
    def test_sub_test_some_fails(self):
        for i in range(3):
            with self.subTest(val=i):
                self.assertEqual(i, 0)

    def test_sub_test_pass(self):
        for i in range(3):
            with self.subTest(val=i):
                self.assertEqual(i, i)

    def test_sub_test_all_fails(self):
        for i in range(3):
            with self.subTest(val=i):
                self.assertEqual(i, 99)
  • Install pytest-subtests plugin (pip install pytest-subtests)
  • run your tests as usual

you should get this kind of output

uv run pytest --odoo-database fdls-test odoo/addons/pytest_add_subtest/ -vvv
2025-07-10 16:15:20,337 611162 WARNING ? py.warnings: /home/pverkest/clients/foodles/odoo/.venv/lib/python3.8/site-packages/odoo/addons/base/models/ir_actions_report.py:73: DeprecationWarning: distutils Version classes are deprecated. Use packaging.version instead.
  if LooseVersion(version) < LooseVersion('0.12.0'):
 
2025-07-10 16:15:20,338 611162 WARNING ? py.warnings: /home/pverkest/clients/foodles/odoo/.venv/lib/python3.8/site-packages/odoo/addons/base/models/ir_actions_report.py:78: DeprecationWarning: distutils Version classes are deprecated. Use packaging.version instead.
  if LooseVersion(version) >= LooseVersion('0.12.2'):
 
================================================================================================================================== test session starts ===================================================================================================================================
platform linux -- Python 3.8.20, pytest-8.3.3, pluggy-1.5.0 -- /home/pverkest/clients/foodles/odoo/.venv/bin/python
cachedir: .pytest_cache
rootdir: /home/pverkest/clients/foodles/odoo
configfile: pyproject.toml
plugins: xdist-3.6.1, github-actions-annotate-failures-0.3.0, cov-3.0.0, odoo-2.1.4.dev6+gab8c896, subtests-0.13.1
collected 3 items                                                                                                                                                                                                                                                                        

odoo/addons/pytest_add_subtest/tests/test_pytest_addsubtest.py::AnExampleCase::test_sub_test_all_fails (val=0) SUBFAIL
odoo/addons/pytest_add_subtest/tests/test_pytest_addsubtest.py::AnExampleCase::test_sub_test_all_fails (val=1) SUBFAIL
odoo/addons/pytest_add_subtest/tests/test_pytest_addsubtest.py::AnExampleCase::test_sub_test_all_fails (val=2) SUBFAIL
odoo/addons/pytest_add_subtest/tests/test_pytest_addsubtest.py::AnExampleCase::test_sub_test_all_fails PASSED
odoo/addons/pytest_add_subtest/tests/test_pytest_addsubtest.py::AnExampleCase::test_sub_test_pass PASSED
odoo/addons/pytest_add_subtest/tests/test_pytest_addsubtest.py::AnExampleCase::test_sub_test_some_fails (val=1) SUBFAIL
odoo/addons/pytest_add_subtest/tests/test_pytest_addsubtest.py::AnExampleCase::test_sub_test_some_fails (val=2) SUBFAIL
odoo/addons/pytest_add_subtest/tests/test_pytest_addsubtest.py::AnExampleCase::test_sub_test_some_fails PASSED

======================================================================================================================================== FAILURES ========================================================================================================================================
_____________________________________________________________________________________________________________________ AnExampleCase.test_sub_test_all_fails (val=0) ______________________________________________________________________________________________________________________

self = <odoo.addons.pytest_add_subtest.tests.test_pytest_addsubtest.AnExampleCase testMethod=test_sub_test_all_fails>

    def test_sub_test_all_fails(self):
        for i in range(3):
            with self.subTest(val=i):
>               self.assertEqual(i, 99)
E               AssertionError: 0 != 99

odoo/addons/pytest_add_subtest/tests/test_pytest_addsubtest.py:18: AssertionError
_____________________________________________________________________________________________________________________ AnExampleCase.test_sub_test_all_fails (val=1) ______________________________________________________________________________________________________________________

self = <odoo.addons.pytest_add_subtest.tests.test_pytest_addsubtest.AnExampleCase testMethod=test_sub_test_all_fails>

    def test_sub_test_all_fails(self):
        for i in range(3):
            with self.subTest(val=i):
>               self.assertEqual(i, 99)
E               AssertionError: 1 != 99

odoo/addons/pytest_add_subtest/tests/test_pytest_addsubtest.py:18: AssertionError
_____________________________________________________________________________________________________________________ AnExampleCase.test_sub_test_all_fails (val=2) ______________________________________________________________________________________________________________________

self = <odoo.addons.pytest_add_subtest.tests.test_pytest_addsubtest.AnExampleCase testMethod=test_sub_test_all_fails>

    def test_sub_test_all_fails(self):
        for i in range(3):
            with self.subTest(val=i):
>               self.assertEqual(i, 99)
E               AssertionError: 2 != 99

odoo/addons/pytest_add_subtest/tests/test_pytest_addsubtest.py:18: AssertionError
_____________________________________________________________________________________________________________________ AnExampleCase.test_sub_test_some_fails (val=1) _____________________________________________________________________________________________________________________

self = <odoo.addons.pytest_add_subtest.tests.test_pytest_addsubtest.AnExampleCase testMethod=test_sub_test_some_fails>

    def test_sub_test_some_fails(self):
        for i in range(3):
            with self.subTest(val=i):
>               self.assertEqual(i, 0)
E               AssertionError: 1 != 0

odoo/addons/pytest_add_subtest/tests/test_pytest_addsubtest.py:8: AssertionError
_____________________________________________________________________________________________________________________ AnExampleCase.test_sub_test_some_fails (val=2) _____________________________________________________________________________________________________________________

self = <odoo.addons.pytest_add_subtest.tests.test_pytest_addsubtest.AnExampleCase testMethod=test_sub_test_some_fails>

    def test_sub_test_some_fails(self):
        for i in range(3):
            with self.subTest(val=i):
>               self.assertEqual(i, 0)
E               AssertionError: 2 != 0

odoo/addons/pytest_add_subtest/tests/test_pytest_addsubtest.py:8: AssertionError
==================================================================================================================================== warnings summary ====================================================================================================================================
odoo/addons/pytest_add_subtest/tests/test_pytest_addsubtest.py::AnExampleCase::test_sub_test_all_fails
  /home/pverkest/clients/foodles/odoo/.venv/lib/python3.8/site-packages/requests_toolbelt/_compat.py:11: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3, and in 3.10 it will stop working
    from collections import Mapping, MutableMapping

odoo/addons/pytest_add_subtest/tests/test_pytest_addsubtest.py::AnExampleCase::test_sub_test_all_fails
  /home/pverkest/clients/foodles/odoo/.venv/lib/python3.8/site-packages/zeep/loader.py:3: DeprecationWarning: defusedxml.lxml is no longer supported and will be removed in a future release.
    from defusedxml.lxml import fromstring

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
================================================================================================================================ short test summary info =================================================================================================================================
(val=0) SUBFAIL odoo/addons/pytest_add_subtest/tests/test_pytest_addsubtest.py::AnExampleCase::test_sub_test_all_fails - AssertionError: 0 != 99
(val=1) SUBFAIL odoo/addons/pytest_add_subtest/tests/test_pytest_addsubtest.py::AnExampleCase::test_sub_test_all_fails - AssertionError: 1 != 99
(val=2) SUBFAIL odoo/addons/pytest_add_subtest/tests/test_pytest_addsubtest.py::AnExampleCase::test_sub_test_all_fails - AssertionError: 2 != 99
(val=1) SUBFAIL odoo/addons/pytest_add_subtest/tests/test_pytest_addsubtest.py::AnExampleCase::test_sub_test_some_fails - AssertionError: 1 != 0
(val=2) SUBFAIL odoo/addons/pytest_add_subtest/tests/test_pytest_addsubtest.py::AnExampleCase::test_sub_test_some_fails - AssertionError: 2 != 0
======================================================================================================================== 5 failed, 3 passed, 2 warnings in 2.49s

closes #69

@codecov-commenter
Copy link

codecov-commenter commented Jul 10, 2025

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

Attention: Patch coverage is 60.00000% with 4 lines in your changes missing coverage. Please review.

Please upload report for BASE (master@e13b04f). Learn more about missing BASE report.

Files with missing lines Patch % Lines
pytest_odoo.py 60.00% 4 Missing ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff            @@
##             master      #86   +/-   ##
=========================================
  Coverage          ?   37.59%           
=========================================
  Files             ?        1           
  Lines             ?      133           
  Branches          ?        0           
=========================================
  Hits              ?       50           
  Misses            ?       83           
  Partials          ?        0           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@petrus-v petrus-v force-pushed the feat-support-addSubTest branch from 065742a to ab8c896 Compare July 10, 2025 15:55
@petrus-v petrus-v marked this pull request as ready for review July 10, 2025 16:22
Copy link

@alexandregaldeano alexandregaldeano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🚀

Copy link
Contributor

@yvaucher yvaucher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

AttributeError: 'TestCaseFunction' object has no attribute 'addSubTest'.
4 participants