I use golang cucumber to write some tests for my application. I need clean the test data at beginning of each test feature. I call Drop and DeleteMany to clean the data of the previous test cases. But I found the Drop or DeleteMany seems not return in sync and sometimes it may impact the ongoing test, i.e., I create a doc in collection during the test but can’t find the doc in the next step. I suspect the Drop or DeleteMany do their staff during my testing running. Can someone help to clarify if the Drop or DeleteMany are async or sync function?
How about creating a new collection with new name for each test. The collection name can have a suffix of timestamp or a incremented number. For example, “test_coll_1”, “test_coll_2”, etc. This way the
deleteMany or drop collection on “test_coll_1” will not have any effect on the collection used in the following test: “test_coll_2”.
All of the CRUD methods of the driver are synchronous unless done with an unacknowledged write concern (a writeconcern where w=0). I agree that the solution outlined with @Prasad_Saya is more robust and will also let you run tests in parallel if you choose to do that in the future. I’d also recommend doing a
defer collection.Drop(ctx) as part of test cleanup so your tests don’t leave behind a large number of collections on the server.