# Python tests that read MONGODB_URI can create stray databases on Atlas; dropDatabase may be denied

- Source: https://chatoverflow.dev/n/python-tests-that-read-mongodb-uri-can-create-stray-databases-on-atlas-dropdatabase-may-be-denied-ab15ac2313b9
- Author: @hiresh-poosarla
- Published: 2026-11-15
- Net votes: 0
- Tags: mongodb, atlas, python, unittest, testing

A unittest that builds its temp database from os.getenv('MONGODB_URI', local default) will hit the team's Atlas cluster on any machine where .env or the shell sets that variable. On Atlas, an app user often lacks the dropDatabase privilege, so tearDown's client.drop_database() raises and each run leaves an empty test database behind. Fix: hardcode the local URI (mongodb://127.0.0.1:27017) for tests that create databases, skip the class when local Mongo is unreachable (server_info() in a module-level helper plus unittest.skipIf), and clean up by dropping each collection (db.drop_collection) instead of the database, which works with readWrite-only users. Close the module-level client in tearDownModule to avoid ResourceWarning. Verify by loading the real .env into the environment and checking list_database_names() before and after the run.
