Skip to content

Latest commit

 

History

History
54 lines (40 loc) · 2.45 KB

avoid-global-test-fixture.md

File metadata and controls

54 lines (40 loc) · 2.45 KB

Avoid global test fixtures and seeds, add data per-test



One Paragraph Explainer

Going by the golden testing rule - keep test cases dead-simple, each test should add and act on its own set of DB rows to prevent coupling and easily reason about the test flow. In reality, this is often violated by testers who seed the DB with data before running the tests (also known as ‘test fixture’) for the sake of performance improvement. While performance is indeed a valid concern — it can be mitigated (e.g. In-memory DB, see “Component testing” bullet), however, test complexity is a much painful sorrow that should govern other considerations. Practically, make each test case explicitly add the DB records it needs and act only on those records. If performance becomes a critical concern — a balanced compromise might come in the form of seeding the only suite of tests that are not mutating data (e.g. queries)



Code example: each test acts on its own set of data

it('When updating site name, get successful confirmation', async () => {
  //Arrange - test is adding a fresh new records and acting on the records only
  const siteUnderTest = await SiteService.addSite({
    name: 'siteForUpdateTest'
  });

  //Act
  const updateNameResult = await SiteService.changeName(siteUnderTest, 'newName');

  //Assert
  expect(updateNameResult).to.be(true);
});



Code Example – Anti Pattern: tests are not independent and assume the existence of some pre-configured data

before(() => {
  //Arrange - adding sites and admins data to our DB. Where is the data? outside. At some external json or migration framework
  await DB.AddSeedDataFromJson('seed.json');
});

it('When updating site name, get successful confirmation', async () => {
  //Arrange - I know that site name 'portal' exists - I saw it in the seed files
  const siteToUpdate = await SiteService.getSiteByName('Portal');

  //Act
  const updateNameResult = await SiteService.changeName(siteToUpdate, 'newName');

  //Assert
  expect(updateNameResult).to.be(true);
});

it('When querying by site name, get the right site', async () => {
  //Act - I know that site name 'portal' exists - I saw it in the seed files
  const siteToCheck = await SiteService.getSiteByName('Portal');

  //Assert
  expect(siteToCheck.name).to.be.equal('Portal'); //Failure! The previous test change the name :[
});