PyTest is running app code outside of designated test functions #10716
-
Normally PyTest scans the code for functions like Here's a small test case that demonstrates this behavior. It will also happen if this code is just in the global scope and without import asyncio
async def some_fn() -> None:
x = {"x": 123}
print(x["y"])
async def main() -> None:
print("*** Running app code!!! ***")
await some_fn()
asyncio.run(main()) Running with this:
, generates this output, which shows that the non-test code is running:
So, the question is - is it the field access error that triggers this behavior or PyTest may routinely run application code outside of any test functions? Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
You run main on import, pytest must import to find functions Protect your call to main by checking |
Beta Was this translation helpful? Give feedback.
-
I see. I never used introspection and incorrectly thought that PyTest could find test functions/classes and would invoke them without running any app code. Thanks for explaining. |
Beta Was this translation helpful? Give feedback.
You run main on import, pytest must import to find functions
Protect your call to main by checking
if __name__ == "__main__" :