-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
tests/run-tests.py: Set name of injected test module to '__main__'. #16433
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tests/run-tests.py: Set name of injected test module to '__main__'. #16433
Conversation
@projectgus @andrewleech @stinos do you have any preference for this fix? The options are:
Maybe option (2) is cleaner, because then it doesn't matter what the module is imported as? |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #16433 +/- ##
=======================================
Coverage 98.57% 98.57%
=======================================
Files 164 164
Lines 21349 21349
=======================================
Hits 21045 21045
Misses 304 304 ☔ View full report in Codecov by Sentry. |
I prefer the way in this PR, it seems "principle of least surprise" that a Python file executed by the test runner is called
IIUC that way will run the tests even if you |
Yes and no. I would think that it doesn't really matter what module a test is imported as, it should just run. Up until now that's been the case, tests just run no matter what. It's only with unittest that things have changed, because of the bit at the bottom of the unittest: if __name__ == "__main__":
unittest.main() Maybe it was an oversight by me to require this code... it's not like these tests are ever going to be imported and used as a module, right? You would always expect them to just run their test. We aren't using auto-discovery here like
Right, if you import the unittest-based test from the REPL then, currently, it won't execute. But if we changed the last bit to read |
Is there anything which might rely on
Even though I kinda dislike the Which in this case would mean for example: editor plugins supporting unittest use discovery to then allow you to run/debug a single test in a file. Pretty sure that's only going to work if the file can be imported as a module. |
My first thought was why is it currently called Back on topic, I think the way presented in this PR is best. The I definitely think it's worth keeping the I do like how clean Speaking of confusing people, the one negative side effect I foresee is if said unit test developer tries to use |
No, that's a private internal name used only by
I did try changing Thanks all for the quick and useful feedback here. Let's go with what's in this PR. |
80cd32a
to
cdb3f82
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
Running unittest-based tests with --via-mpy is currently broken, because the unittest test needs the module to be named `__main__`, whereas it's actually called `__injected_test`. Fix this by changing the name when the file is opened. Signed-off-by: Damien George <damien@micropython.org>
cdb3f82
to
e323da7
Compare
Summary
Running unittest-based tests with
--via-mpy
is currently broken, because the unittest test needs the module to be named__main__
, whereas it's actually called__injected_test
.Fix this by changing the name when the file is opened.
Testing
The following now works:
Trade-offs and Alternatives
This fix is a little hacky.
An alternative would be to just make unittest-based tests do this at the end of the file:
(Instead of
if __name__ == '__main__': unittest.main()
).