Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automated Testing with Databases

I'm fairly new to automated testing and was wondering how I should go about writing tests for the database. The project I'm working on right now is running PostgreSQL with Sequelize as the ORM on a Node.JS environment. If it matters, I'm also using Jest as the testing library right now.

like image 861
Calvin Avatar asked Oct 15 '17 20:10

Calvin


People also ask

Can database testing be automated?

These tests can either be fully automated, fully manual, or a hybrid approach using a mix of both manual and automated processes. For example, in a fully manual test, you could go into the database management system and run queries to validate assumptions.

Which database is used in automation testing?

Selenium Database Connection Selenium is one of the prominent automation testing tools.

Is SQL used for automation testing?

As a software tester, you are required to perform database testing that requires the knowledge of different SQL and database concepts. In addition, you are required to write SQL queries to retrieve, update and insert data in the databases.


1 Answers

In my app I use a config module to control configuration settings for different environments. When running tests the process.env.APP_ENV is set to test, and it will set the dialect to sqlite. Note that you will not have any data or data persistence, so you will need to populate it with all the data needed for your tests.

Include sqlite3

yarn add -D sqlite3

or

npm i -D sqlite3

Config

module.exports = {
  database: {
    name: 'dbname',
    user: 'user',
    password: 'password',
    host: 'host',
    // Use "sqlite" for "test", the connection settings above are ignored
    dialect: process.env.APP_ENV === 'test' ? 'sqlite' : 'mysql',
  },
};

Database/Sequelize

// get our config
const config = require('../config');

... // code

const instance = new Sequelize(
    config.database.name,
    config.database.user,
    config.database.password,
    {
      host: config.database.host,
      // set the dialect, will be "sqlite" for "test"
      dialect: config.database.dialect,
    }
);

Test Class (Mocha)

const TestUtils = require('./lib/test-utils');

describe('Some Tests', () => {
  let app = null;

  // run before the tests start
  before((done) => {
    // Mock up our services
    TestUtils.mock();

    // these are instantiated after the mocking
    app = require('../server');

    // Populate redis data
    TestUtils.populateRedis(() => {
      // Populate db data
      TestUtils.syncAndPopulateDatabase('test-data', () => {
        done();
      });
    });
  });

  // run code after tests have completed
  after(() => {
    TestUtils.unMock();
  });

  describe('/my/route', () => {
    it('should do something', (done) => {
      return done();
    });
  });
});

Run Tests

APP_ENV=test ./node_modules/.bin/mocha

You could use ENV variables in other ways to set the dialect and connection parameters as well - the above is just an example based on what we have done with a lot of supporting code.

like image 50
doublesharp Avatar answered Sep 28 '22 09:09

doublesharp