A warning on setUpClass in our python tests

Andrew Bartlett abartlet at samba.org
Tue Aug 31 00:45:18 UTC 2021


I wanted to use setUpClass() to create once a user account for some
tests (user_account_control.py), to avoid making a user over and over
again.

However it is important to note that unless the test is under
python/samba/tests and loaded with planpythontestsutie any exceptions
raised in the setUpClass() are not caught.

The result is that ALL THE TESTS ARE SILENTLY SKIPPED.

Not ideal.  Thankfully we don't do this, but there is a real risk that
we might have a testsuite that silently vanishes.  The official docs
don't mention this here:
https://docs.python.org/3/library/unittest.html#setupclass-and-teardownclass

Hoever there is this note here:
https://docs.python.org/3/library/unittest.html#class-and-module-fixtures
which indicates that a _ErrorHolder object is created.  We might need
to find a way to locate that if we don't just move away from tests as
executable scripts.

Others have seen this (trying to skip):
https://github.com/CleanCut/green/discussions/215#discussioncomment-188017

Their monkey-patch looks horrible:
https://github.com/CleanCut/green/commit/9f393edcaacc838bf3635f84a83537a6c7ce045b

But vaugely mention that _Error

Medium term it would be really good if, source4/dsdb/tests/python was
moved to python/samba/tests/dsdb and the invocations changes to use
planpythontestsuite, as it works there.

(BTW, with Python 3.8 we gain addClassCleanup() which would be
helpful). 

TLDR: avoid setUpClass unless using planpythontestuite.


Andrew Bartlett
-- 
Andrew Bartlett (he/him)       https://samba.org/~abartlet/
Samba Team Member (since 2001) https://samba.org
Samba Team Lead, Catalyst IT   https://catalyst.net.nz/services/samba

Samba Development and Support, Catalyst IT - Expert Open Source
Solutions
-------------- next part --------------
A non-text attachment was scrubbed...
Name: setUpClass.patch
Type: text/x-patch
Size: 4431 bytes
Desc: not available
URL: <http://lists.samba.org/pipermail/samba-technical/attachments/20210831/f63aacf3/setUpClass.bin>


More information about the samba-technical mailing list