//

MongoDB for the robot

6.6.2013 | 2 minutes of reading time

We use the Robot Framework for some time for automated software tests in our projects. In addition, a few of my colleagues deal with the NoSql Database MongoDB (Tutorial MongoDB ). The document management solution Center-Device (Start-Up of Codecentric AG) also uses MongoDB in the backend (Center-Device Architecture-Overview ). So I thought, it’s time for a new Robot-Library: robotframework-mongodblibrary . The version 0.2.1 is ready for download on GitHub (Keyword-Documentation ).

The library is written in Java (Jython) and uses internally the MongoDB Java Driver. Despite limited experience, programming the library was very easy. Thx for the great MongoDB-Documentation 🙂

the first robot test

1*** Settings ***
2Library de.codecentric.robot.mongodblibrary.keywords.MongodbLibrary
3Test Setup Setup MongoDB
4 
5*** Test Cases ***
6should insert given document
7     Insert Document myCollection {say : 'Hello MongoDb!'}
8     Collection Should Exist myCollection
9     Document Should Exist myCollection {say : 'Hello MongoDb!'}
10 
11*** Keywords ***
12Setup MongoDB
13     Connect To Server localhost 27020 robotdb1
14     Drop Database robotdb1
15

Robot Remote Library

The robot remote library is also on board 🙂 Then you can start the library as a separate server. This is very useful if you want to use python instead of jython in the main test suite.

1*** Settings ***
2Library   Remote    http://localhost:8270
3Test Setup Setup MongoDB
4 
5*** Test Cases ***
6should insert given document
7     Insert Document myCollection {say : 'Hello MongoDb!'}
8     Collection Should Exist myCollection
9     Document Should Exist myCollection {say : 'Hello MongoDb!'}
10 
11*** Keywords ***
12Setup MongoDB
13     Connect To Server localhost 27020 robotdb1
14     Drop Database robotdb1
15

Before starting the tests you need to start the server by the folowing shell command:

1java -jar build/libs/robotframework-mongodblibrary-0.2.1-with-dependencies.jar --port 8270
2

MongoDB-Server Embedded

Since the 0.2-Version there is a keyword that starts a MongoDB-Server (of a given Version) within the tests . This is done by the Embedded MongoDB Library . The keyword downloads the server from the MongoDB-Webpage , extracts this to the home directory und starts the MongoDB Daemon in a seperate oprating system process (MongoDB is written in C++). At the end of test the daemon should be terminated by the keyword Shutdown Embedded , that’s a clean shutdown of the database.

1*** Settings ***
2Library de.codecentric.robot.mongodblibrary.keywords.MongodbLibrary
3Suite Setup  Startup Embedded  2.4.4
4Suite TearDown  Shutdown Embedded
5Test Setup Setup MongoDB
6 
7*** Test Cases ***
8should insert given document
9     Insert Document myCollection {say : 'Hello MongoDb!'}
10     Collection Should Exist myCollection
11     Document Should Exist myCollection {say : 'Hello MongoDb!'}
12 
13*** Keywords ***
14Setup MongoDB
15     Connect To Server localhost 27020 robotdb1
16     Drop Database robotdb1
17

One of the main benefits of the server integration is the resulting clarity of the Robot-Logs. The log messages of the MongoDB Server are written to the Robot-Log and can directly assigned to the keywords:

Usage in other testing frameworks

The library has no dependencies to the robotframework, so you can also use the library with other testing frameworks (e.g. Junit , JBehave ). For this approach there is another download version without dependencies .

have fun with the library 🙂

share post

Likes

0

//

More articles in this subject area\n

Discover exciting further topics and let the codecentric world inspire you.

//

Gemeinsam bessere Projekte umsetzen

Wir helfen Deinem Unternehmen

Du stehst vor einer großen IT-Herausforderung? Wir sorgen für eine maßgeschneiderte Unterstützung. Informiere dich jetzt.

Hilf uns, noch besser zu werden.

Wir sind immer auf der Suche nach neuen Talenten. Auch für dich ist die passende Stelle dabei.