This repository has been archived by the owner on Jan 5, 2024. It is now read-only.
test, dev: fix mocking in test suite, enable dev to run single tests, get coverage report #31
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Backstory
When I went to do some bug fixes on Friday, I first ran the test suite on
master
and found that the test suite was failing locally for me - this PR description contains the main problems I hit and how they are resolved here.Problem 1: defining but not applying our inline
unittest.mock.patch
mocksThe first error I hit was due to
lsblk
not being present on my system (i was on a mac while doing this which doesn’t havelsblk
). Investigating further, I was surprised to see that the test failed since the subprocess call tolsblk
was mocked.That’s when I realized the problem: in places we were using
unittest.mock.patch
inline but not calling.start()
on the mocks. If we don’t do this, they won’t be applied: see this branch which has the mocks started which causes many test failures.Fix: we can use
pytest-mock
for our inline mocks: when we use this we just need to use themocker
pytest fixture and the mock will be started and stopped for us.Problem 2: mocking in the wrong place
In that branch’s test output we see the second issue - we have
export.exit_gracefully
mocked - butexit_gracefully
is a method onSDExport
, not a module-level function (this is seen via the test output above when we try to start the mock) . Whensubmission
is an instance ofSDExport
we should mock theexit_gracefully
method on thesubmission
object.Considering the logic in the asserts here, it doesn’t make sense: if
exit_gracefully
is properly mocked (wheresys.exit(0)
is occurring most of the time) then we should not need to assert that aSystemExit
is raised unless we includesys.exit(0)
as a side effect of the mockedexit_gracefully
. This lets us know that the mock isn't working.It’s OK to mock out the
exit_gracefully
method calls since there are unit tests for that method which cover both the exception / no exception cases.If you're wondering: but... we had asserts on
mocked_exit
- why did the asserts pass? It turns out they didn't - this test run based on a one line change here demonstrates this. This was masked due to the fact they were in thepytest.raises
context and were not executed. See this comment in the pytest source code for what is going on. The remaining uses ofpytest.raises
in this PR do not have additional asserts so we should be good now on this front but it's something to be aware of.Fix: we ensure that we’re mocking
exit_gracefully
in the right place and removing the unnecessary asserts (that will not pass due to no longer running the logic insideexit_gracefully
)other dev/test improvements
I added some other minor dev agility and test improvements in the course of working on this (let me know if you want me to break this up for easier review, can do so):
dev:
test:
side_effect
(instead ofreturn_value
) so that exceptions are raisedanyway please let me know if you think I misunderstood anything here!