jeudi 14 novembre 2019

Properly write integration tests for database population logic

I have an event handler written in node which should populate several MySQL tables as a result of a single transaction (meaning that all the operations should complete correctly or otherwise everything must be rollbacked to the initial state). I have some doubts about the correct way to write integration tests in this case. The followings are some possible scenarios I was thinking:

1) Run the population logic in a before all tests phase and then run individual tests on tables, then cleanup everything in a after all tests phase.

describe('test', function() {
    before(async () => {
        // run event handler to populate database
    });

    after(async() => {
        // cleanup
    });

    it('test Table A', () => {  })

    it('test Table B', () => {  })

    it('test Table C', () => {  })
});

2) Run population logic and cleanup before and after each test respectively.

describe('test', function() {
    beforeEach(async () => {
        // run event handler to populate database
    });

    afterEach(async() => {
        // cleanup
    });

    it('test Table A', () => {  })

    it('test Table B', () => {  })

    it('test Table C', () => {  })
});

3) Test everything in one single test.

describe('test', function() {
    before(async () => {
        // just open the db connection
    });

    after(async() => {
        // cleanup
    });

    it('populate db and test every table', () => {  })
});

There are other possible scenarios as well. I would like to know which is the better way to go and why.

ps: I am using mocha and chai for testing but the question should be language agnostic.

Aucun commentaire:

Enregistrer un commentaire