unittests: add magic flag global to integrate utility methods with unittest

The stdlib unittest module has a magic flag (undocumented) which
indicates that a module is part of a unittest framework.

> Truncates usercode tb at the first unittest frame.
>
> If the first frame of the traceback is in user code,
> the prefix up to the first unittest frame is returned.
> If the first frame is already in the unittest module,
> the traceback is not modified.

This avoids some ugliness, e.g. the following test error logs:

```
>       self.assertPathListEqual(intro[0]['install_filename'], ['/usr/lib/libstat.aaa'])

unittests/allplatformstests.py:432:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
unittests/baseplatformtests.py:393: in assertPathListEqual
    self.assertPathEqual(i[0], i[1])
unittests/baseplatformtests.py:384: in assertPathEqual
    self.assertEqual(PurePath(path1), PurePath(path2))
E   AssertionError: PurePosixPath('/usr/lib/libstat.a') != PurePosixPath('/usr/lib/libstat.aaa')
```

Since assertPathListEqual is our own assertion helper, we don't need to
give trace information about its internals. This change causes the error
log to become:

```
>       self.assertPathListEqual(intro[0]['install_filename'], ['/usr/lib/libstat.aaa'])
E       AssertionError: PurePosixPath('/usr/lib/libstat.a') != PurePosixPath('/usr/lib/libstat.aaa')

unittests/allplatformstests.py:432: AssertionError
```

which is a lot more readable.
pull/11530/head
Eli Schwartz 2 years ago
parent 4e17d60d47
commit 9dbe718eb6
No known key found for this signature in database
GPG Key ID: CEB167EFB5722BD6
  1. 5
      unittests/baseplatformtests.py

@ -45,6 +45,11 @@ from run_tests import (
)
# magic attribute used by unittest.result.TestResult._is_relevant_tb_level
# This causes tracebacks to hide these internal implementation details,
# e.g. for assertXXX helpers.
__unittest = True
class BasePlatformTests(TestCase):
prefix = '/usr'
libdir = 'lib'

Loading…
Cancel
Save