Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integration Tests Against a Database - AVA

Tags:

ava

After writing unit tests, i'm facing with Integration tests, which consist of testing the library against a database (rethinkdb).

Each testcase has to be independent between each other, and the database will be cleared after each test, so that they will not give false positives.

Because of AVA architecture is to run tests in parallel, i found out that I can't achieve positive tests in that manner. ex:

test.beforeEach(async function(t) {

  const users = t.context.users = await DB.models.User.save([
    {name: 'jhon',username: 'doe'},
    {name: 'fabri',username: 'fenos'},
    {name: 'will',username: 'red'},
    {name: 'smith',username: 'blue'},
    {name: 'paul',username: 'orange'},
    {name: 'tesla',username: 'ele'},
  ]);

  t.context.tasks = await DB.models.Task.save([
    {title: 'My task1', description: 'My duty1', assignee_id: _.sample(users).id},
    {title: 'My task2', description: 'My duty2', assignee_id: _.sample(users).id},
    {title: 'My task3', description: 'My duty3', assignee_id: _.sample(users).id},
  ]);
});

test.afterEach(async (t) => {
  return await DB.clearDB();
});

I been forced to use the serial function to allow every test to finish and clear the DB in a serial way.

My tests might update or remove of data which may cause false positives if the test share the same data at same time.

If the tests that i'm writing are just reading data, i could just do the operation of seeding and cleaning just as pre and post script as mentioned in the issue #311 and keep the tests in parallel.

I also found very nice, the way i can use the t.context and pass down users / tasks objects into my tests.

Is it this one, the rare case that we are forced to use serial test execution?

How would you tackle this kind of tests using ava?

like image 252
Fabrizio Fenoglio Avatar asked May 05 '16 09:05

Fabrizio Fenoglio


People also ask

What is integration test in database?

Integration tests focus on testing how separate parts of the program work together. In the context of applications using a database, integration tests usually require a database to be available and contain data that is convenient to the scenarios intended to be tested.

What is integration testing in SQL?

Integration testing is done to test the modules/components when integrated to verify that they work as expected i.e. to test the modules which are working fine individually does not have issues when integrated.

Why do integration tests fail?

For instance, integration tests could fail due to a lost connection with the database. Or perhaps you're using the same environment for both testing and development; in that case, the developers may push new code without tests, causing the test environment to collapse.


1 Answers

Yes, test.serial is meant for this.

Alternatively if you could use a different database for each test that would allow you to run them in parallel.

like image 118
Mark Wubben Avatar answered Nov 01 '22 17:11

Mark Wubben