1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
I tend to always use
IsolatedAsyncioTestCase - that's probably only necessary
if you're testing async code, but maybe I'm lazy :D
platform from the library I'm testing rather than importing it directly.
This ensures that if something changes with the library (for example, if we switch
os), we'll immediately notice that we're using something
that isn't in the library.
Line 7 is where we begin the patch for
platform.uname. If you aren't familiar
patch works, it's going to create a
MagicMock for that function. We're
concerned with that that function returns. We set that by setting
which is passed to the
In order to ensure that we're not testing too much, we ant to make sure we only
set the attributes of the
platform.uname return value that we use in our function.
In this case, we care about
node. We use
create_autospec to ensure
that our return value mimics a
platform.uname_result. Additionally, this will
ensure our unit test fails if the API changes and one of the attributes we care
about doesn't exist any more.
That last sentence is really the important thing here. If we just use a
we run the risk of masking an API change. Or, if we actually just use
platform.uname_result instead, we run the risk of needlessly breaking a unit
test if the API changes in a way that doesn't impact our test (for example, if a
required attribute is added).
Check out Autospeccing in the Python docs.