Skip to content

Support @expectedFailure in unittest#1139

Open
Josverl wants to merge 2 commits into
micropython:masterfrom
Josverl:unittest_expectedfailure_direct
Open

Support @expectedFailure in unittest#1139
Josverl wants to merge 2 commits into
micropython:masterfrom
Josverl:unittest_expectedfailure_direct

Conversation

@Josverl

@Josverl Josverl commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

Summary

I have found that in testing MicroPython it is sometimes hard to keep track of what is correct for Python, and what is correct for MicroPython.
We want the CI to remain green - to detect and avoid regressiobns, and MicroPython small , and close to CPython.

In MicroPython tests that means that we sometimes need to test for not CPython behaviour.
That can be done with .exp files for non-unittest files.

CPython's unittest has a @unittest.expectedFailure decorator that can be used to differentiate between:

  • unittests that pass because they are CPython compatible
  • unittests that pass because they match MicroPython's minimalism

Note

This differentiation is not magically inserted , the test code still needs to be good.

I have used this approach when running matrix tests against the different typing implementations, one example where it was clear that a 100% CPython compatibility was a non-goal.
By decorating some tests with @unittest.expectedFailure a much cleared report can be built on the capabilities and correctness for both MicroPython and CPython.

This PR implements this directly as part of the unittest module to keep usage as simple as possible.

import unittest

class MyTests(unittest.TestCase):
    @unittest.expectedFailure
    def test_known_difference(self):
        self.assertEqual(unsupported(), 42)  # currently fails; that's expected

if __name__ == "__main__":
    unittest.main()

impact on size

Measured with mpy-cross -O3 (v1.27, mpy v6.3), using the same source filename for a fair comparison:

Build unittest.mpy (-O3)
master baseline 4974 bytes
this PR 5387 bytes
difference +413 bytes (+8.3%)

Testing

Added test to test the unitest.

Trade-offs and Alternatives

This is an alternative implementation to PR #1133

Generative AI

I used generative AI tools when creating this PR, but a human has checked the
code and is responsible for the code and the description above.

@Josverl Josverl requested a review from dpgeorge July 15, 2026 20:06
@Josverl Josverl force-pushed the unittest_expectedfailure_direct branch 2 times, most recently from 3d81359 to 005e402 Compare July 15, 2026 20:55
@Josverl

Josverl commented Jul 15, 2026

Copy link
Copy Markdown
Contributor Author

Found a refactor to save 71 bytes; now at +342 B (8.3% → 6.9%)

There are a few more options ; reduce the ClassNames names to be shorter but more cryptic would save ~24 bytes.

Josverl added 2 commits July 15, 2026 23:19
Includes unittest for the new functionality.

Signed-off-by: Jos Verlinde <Jos_Verlinde@hotmail.com>
Signed-off-by: Jos Verlinde <Jos_Verlinde@hotmail.com>
@Josverl Josverl force-pushed the unittest_expectedfailure_direct branch from 0e7891b to 207bfd4 Compare July 15, 2026 21:20
@Josverl Josverl added the enhancement Feature requests, new feature implementations label Jul 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Feature requests, new feature implementations

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant