tests: centralize transport and session cleanup in conftest#1683
tests: centralize transport and session cleanup in conftest#1683ZeliardM wants to merge 1 commit intopython-kasa:masterfrom
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1683 +/- ##
==========================================
+ Coverage 93.22% 93.23% +0.01%
==========================================
Files 157 157
Lines 9815 9815
Branches 1003 1003
==========================================
+ Hits 9150 9151 +1
+ Misses 472 471 -1
Partials 193 193 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
This PR improves the test suite’s resource hygiene by adding automatic cleanup of transport- and client-owned aiohttp sessions, preventing ResourceWarning: unclosed ... warnings and reducing the chance of session leaks across async tests.
Changes:
- Added per-module
autousefixtures to track created transport instances and close them during teardown. - Added an
autousefixture intests/test_httpclient.pyto close anyHttpClientinstances created by tests. - Replaced several unclosed
aiohttp.ClientSession()usages intests/test_deviceconfig.pywith a shared fixture that ensures sessions are closed.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
tests/transports/test_ssltransport.py |
Add autouse fixture to track/close SslTransport instances created in tests. |
tests/transports/test_sslaestransport.py |
Add autouse fixture to track/close SslAesTransport instances created in tests. |
tests/transports/test_linkietransport.py |
Add autouse fixture to track/close LinkieTransportV2 instances created in tests. |
tests/transports/test_klaptransport.py |
Add autouse fixture to track/close AesTransport/KlapTransport/KlapTransportV2; remove an unclosed bare session injection; explicitly close protocol in test_query. |
tests/transports/test_aestransport.py |
Add autouse fixture to track/close AesTransport instances created in tests. |
tests/test_httpclient.py |
Add autouse fixture to track/close HttpClient instances created in tests. |
tests/test_deviceconfig.py |
Introduce http_client fixture to ensure aiohttp.ClientSession is closed; wire it into affected async tests. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
0b37ca9 to
53fcd98
Compare
|
@rytilahti Ok, here is a much better handling for the tests and the httpclient.py sessions and clients. |
Add a single autouse fixture in tests/conftest.py that tracks all BaseTransport and HttpClient instances via __init__ monkeypatching, then closes them after each test. Uses functools.wraps to preserve original __init__ signatures so existing inspect-based tests continue to pass.
53fcd98 to
22121ef
Compare
Summary
Add a centralized autouse fixture in
tests/conftest.pythat automatically tracks and cleans up allBaseTransport,HttpClient, andaiohttp.ClientSessioninstances created during tests. This prevents resource leaks (unclosed sessions/transports) from polluting test output with warnings.Changes
tests/conftest.py— New_close_transport_and_http_sessionsautouse fixture:BaseTransport.__init__,HttpClient.__init__, andaiohttp.ClientSession.__init__to track all created instancesBaseTransportpatch point (all callsuper().__init__())aiohttp.ClientSession()calls acrosstest_deviceconfig.py,test_discovery.py,test_device_factory.py, and transport tests)tests/transports/test_klaptransport.py— Remove dead code:protocol._transport.http_client = aiohttp.ClientSession()intest_handshake— this set a nonexistent attribute (the transport uses_http_client, nothttp_client), creating a dangling session that was never usedFiles changed (2)
tests/conftest.py,tests/transports/test_klaptransport.pyMerge order
This is PR 4 of 9 in the test modernization series. Must merge before #1684 and #1688 (which modify conflicting lines in the same files).
Verification