modernize tests #2777
Labels
No Label
0.2.0
0.3.0
0.4.0
0.5.0
0.5.1
0.6.0
0.6.1
0.7.0
0.8.0
0.9.0
1.0.0
1.1.0
1.10.0
1.10.1
1.10.2
1.10a2
1.11.0
1.12.0
1.12.1
1.13.0
1.14.0
1.15.0
1.15.1
1.2.0
1.3.0
1.4.1
1.5.0
1.6.0
1.6.1
1.7.0
1.7.1
1.7β
1.8.0
1.8.1
1.8.2
1.8.3
1.8β
1.9.0
1.9.0-s3branch
1.9.0a1
1.9.0a2
1.9.0b1
1.9.1
1.9.2
1.9.2a1
LeastAuthority.com automation
blocker
cannot reproduce
cloud-branch
code
code-dirnodes
code-encoding
code-frontend
code-frontend-cli
code-frontend-ftp-sftp
code-frontend-magic-folder
code-frontend-web
code-mutable
code-network
code-nodeadmin
code-peerselection
code-storage
contrib
critical
defect
dev-infrastructure
documentation
duplicate
enhancement
fixed
invalid
major
minor
n/a
normal
operational
packaging
somebody else's problem
supercritical
task
trivial
unknown
was already fixed
website
wontfix
worksforme
No Milestone
No Assignees
4 Participants
Notifications
Due Date
No due date set.
Reference: tahoe-lafs/trac-2024-07-25#2777
Loading…
Reference in New Issue
No description provided.
Delete Branch "%!s(<nil>)"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
I'd like to clean up our unit tests.. there are a lot of archaic idioms that have crept in (partly because I'm not up-to-date on modern practices, partly because the tests were initially written before those practices were established). Here are some things that come to mind:
mock
in favor of proper test doubles]ticket:3519assertEqual(...)
) with "hamcrest" style assertions (assertThat(x, Equals(y))
)async def
/await
to flatten the control-flow nest of helper functions#2465 is where we removed "mock" about 8 months ago (mostly because it wanted a modern setuptools, but we had zetuptoolz, a reason that has gone away).
It also might be fruitful to look at changing at least some of the *MixIn "helper" classes to be 'just' helper methods instead.
By which I mean that if a test-suite wants a particular helper, instead of inheriting from the MixIn and using
self.{various state}
idioms, it might be better to have a "create_whatever_helper" and assign it toself.whatever
in thesetUp
of any test-suites that need them.For example, to use the "no network" test helper, you currently:
GridTestMixin
setUp
properlyself.set_up_grid
at some point (but after the above)self.g
and friends (which are set inset_up_grid
) or any of the dozen methods added toself
by the mix-inInstead, it might be more-clear to take the functionality of
set_up_grid
and make it a bare method that creates you some helper object off of which hang any interesting helper methods for the test. Something like:class [GridTestMixin](wiki/GridTestMixin)
intoclass [NoNetworkGrid](wiki/NoNetworkGrid)
set_up_grid
into a bare factory method that creates, initializes and returns aNoNetworkGrid
(could be async if needed)Then, to use this a test-suite would:
self.grid = create_no_network_grid()
in itssetUp
self.grid.*
Glyph also pointed me at newer assertion methods, like:
f = self.failureResultOf(d, *expectedExceptionTypes)
: passes iff the Deferred has already errbacked, with one of the given typesres = self.successResultOf(d)
: passes iff the Deferred has already callbackedself.assertNoResult(d)
: passes iff the Deferred has not been fired yetThe general pattern is to avoid waiting for Deferreds as much as possible. You call the method that returns a Deferred (with mocks in place for everything it might wait upon), then you manually trigger those mocks. Before, during, and after the triggers, you keep inspecting the Deferred to make sure it's in the right state.
Oh, and for reference, the signature of assertFailure (when using inlineCallbacks) is:
e = yield self.assertFailure(d, *expectedFailures)
: wait ford
to errback, require that it errbacked with one of the given exception types, and yield the exception. Fail ifd
is callbacked instead of being errbacked.In 84a1064/trunk:
modernize tests, use "mock"to modernize testsI edited this to reverse the requirement of using mock to a requirement of not using mock. mock is a unit testing anti-pattern and it should be removed from tahoe's test suite completely.
I'm intrigued (and out-of-date, as usual).. do you have a pointer handy on how "testing doubles" work, vs Mock?
Maybe https://nedbatchelder.com/blog/201206/tldw_stop_mocking_start_testing.html to start
Also relevant
Splitting the mock part of this off into https://tahoe-lafs.org/trac/tahoe-lafs/ticket/3519
I updated the point about
inlineCallbacks
to instead talk aboutasync def
andawait
because the latter is increasingly widely used and so it makes the codebase more approachable for non-Twisted experts.