Testing devices and starting the MongoDb server

I am running a few unit test that save documents in a MongoDb database. For this unit test to succeed, you must start the MongoDb server. I am doing this with Process.Start ("mongod.exe").

It works, but sometimes it takes time to start, and before it even starts to run the unit test, it does NOT FEED. unit test fails and complains that the mongodb server is not running.

What to do in such a situation?

+4
source share
5 answers

Can you run a quick test query in a loop with a delay after starting and verify that the database is before proceeding?

+1
source

If you use an external resource (DB, web server, FTP, backup device, server cluster), then this is more an integration test, and then a unit test. It is not convenient and impractical to run all external resources in the test. Just make sure your test runs in a predictable environment. There are several ways to do this:

  • Run a test suite from a script (BAT, nant, WSC) that runs MongoDB before running the test.
  • Run MongoDB on the server and never close down.

Do not add loops with delays in the tests to wait for the external resource to start - this makes the tests slow, unstable and very complex.

+4
source

Actually easier :): just run mongodb as a service:
How to start MongoDB as a windows service?

This is super easy, with the exception of some OSs, you play with some registry settings to start the service, you can search and find an article about this driving. Let me know if you need any help.

+2
source

I assume that (and I mean, this is what I did, but there is every chance that someone has a better idea) write some kind of MongoTestHelper that can do several things at different stages of your tests.

Before starting the test, he checks that the test mongod instance is working, and, if not, downloads it to his favorite test mongo port. I believe that this is actually not so expensive, just try and download a new instance of mongod and let it fail, because this port is already in use. However, this is very different from windows, so you can check that the port is open or something like that.

Before each individual test, you can remove all items from all verified collections, if that is what you need. In fact, I'm just throwing all the databases, as the beautiful mongodb recreates them for you:

for (String name : mongo.getDatabaseNames()) { mongo.dropDatabase(name); } 

After running the tests, you can always close it if you chose to boot on a random port, but that seems a little silly. Life is too short.

+1
source

TDD purists will say that if you run an external resource, then this is not a unit test. Instead, flush the database interface and test your classes against it. In practice, this would mean turning your code into a layout, which is probably good.

OTOH, in order to write an integration test or acceptance test, you must use a temporary memory exchange database that contains only your test data, as others have noted.

0
source

Source: https://habr.com/ru/post/1311265/


All Articles