Skip to content

Pre and Post Test

James Richford edited this page Feb 13, 2017 · 4 revisions

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

import { Expect, Test, AsyncSetup, Setup, AsyncSetupFixture, 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
  }
  
  @AsyncSetupFixture
  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
  }

  @AsyncSetup
  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, AsyncTeardown, Teardown, AsyncTeardownFixture, 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
  }
  
  @AsyncTeardownFixture
  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
  }

  @AsyncTeardown
  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