Skip to content

Pre and Post Test

James Adarich edited this page Aug 1, 2019 · 4 revisions

You can get a function to be run before every function in the fixture using the Setup decorators

import { 
   Expect,
   Test,
   Setup,
   SetupFixture,
   TestFixture } from "alsatian";

@TestFixture("Example Test Fixture")
export class ExampleTestFixture {
  
  @SetupFixture
  public setupFixture() {
    // this function will be run ONCE before any test has run
    // you can use this to do setup that needs to happen only once
  }
  
  @SetupFixture
  public async asyncSetupFixture() {
    // this function will be run asynchronously ONCE before any test has run
    // you can use this to do asynchronous setup that needs to happen only once
    // NB: the first test will be run after this function completes
  }
  
  @Setup
  public setup() {
    // this function will be run before EVERY test
    // you can use this to do setup that every test will need
  }

  @Setup
  public async asyncSetup() {
    // this function will be run asynchronously before EVERY test
    // you can use this to do asynchronous setup every test will need
    // NB: a test will only be run after this function completes
  }

  @Test()
  public exampleTest() {
   Expect(1).toBe(1);
  }
}

You can also run functions after every test has completed using the Teardown decorators

import {
   Expect,
   Test,
   Teardown,
   TeardownFixture,
   TestFixture } from "alsatian";

@TestFixture("Example Test Fixture")
export class ExampleTestFixture {
  
  @TeardownFixture
  public teardownFixture() {
    // this function will be run ONCE after any test has run
    // you can use this to do teardown that needs to happen only once all tests complete
  }
  
  @TeardownFixture
  public async asyncTeardownFixture() {
    // this function will be run asynchronously ONCE after any test has run
    // you can use this to do asynchronous teardown that needs to happen only once all tests complete
    // NB: the next fixture will be run after this function completes
  }
  
  @Teardown
  public teardown() {
    // this function will be run after EVERY test
    // you can use this to do teardown that every test will need
  }

  @Teardown
  public async asyncTeardown() {
    // this function will be run asynchronously after EVERY test
    // you can use this to do asynchronous teardown every test will need
    // NB: the next test will only be run after this function completes
  }

  @Test()
  public exampleTest() {
   Expect(1).toBe(1);
  }
}
Clone this wiki locally