Skip to content

Fixtures

itaiag edited this page Aug 9, 2016 · 1 revision

Fixtures

One of the most important guidelines in automation development is separating configuration code from testing code. Separating the code allows for reusable configuration code, producing tests that are simple to read and write. These simple tests should be written as independent stand alone tests that are not configured with interdependent characteristics within a single scenario. Separating test code from configuration code is done by writing fixtures. The fixture is a special type of test class that brings the SUT to a specific configuration state.

Note: A naive separation between the configuration and testing environments that keeps on testing independently with reoccurring configurations.

Warning: The result is a waste of time and in many cases wears out the device being tested.

Fixtures Module

In addition to supporting separation of configuration code from testing code, JSystem's fixtures module eliminates inefficient and time consuming testing practices and needless device wear and tear by performing the following functions:

  • A fixture is a special type of test class that brings the SUT to the desired state

  • Each fixture performs an SUT update and or configuration operation, and should know how to rollback and or cleans up after an operation has been performed.

  • Fixtures behave in a symbiotic relationship to one and other and can therefore be ordered in to a hierarchical structure.

  • Each test associate itself with a fixture. While performing a runtime, the framework automatically navigates to the fixture which is required by the test.

Writing a Fixture

JSystem fixtures are used to extend the jsystem.framework.fixture.Fixture class and are required in order to implement a setup method an optional “tearDown” method and a “failTearDown” method.

Fixture Class Code Example

package com.aqua.services.fixtures;
import jsystem.framework.fixture.Fixture;

public class ExampleFixture extends Fixture {

    public ExampleFixture() {
    }
    
    public void setUp() throws Exception {
        report.step(" in example fixture setup");
    }
    
    public void tearDown() throws Exception {
        report.step(" in example fixture tearDown");
    }

    public void failTearDown() throws Exception {
    }
}

The setup method is activated when navigating to the fixture; the “tearDown” method is activated when navigating from the fixture to another fixture. The “failTearDown” method is activated when navigating to another fixture after test failure.

Creating Fixtures Hierarchy

In order to create a fixture hierarchy the programmer must set the fixture’s parent by calling the setParentFixture method in the fixture constructor.

package com.aqua.services.fixtures;

import jsystem.framework.fixture.Fixture;

public class ExampleFixture extends Fixture {

    public ExampleFixture() {
        setParentFixture(ParentExampleFixture.class);
    }
    
    public void setUp() throws Exception {
        report.step(" in example fixture setup");
    }
    
    public void tearDown() throws Exception {
        report.step(" in example fixture tearDown");
    }
    
    public void failTearDown() throws Exception {
    }
  }

Note: When the programmer creates a loop in the fixtures hierarchy (F1->F2->F3->F1), the system identifies the loop and as a result the test execution fails.

Associating a Test with a Fixture

In order to signal the system to navigate to a certain fixture before running the test in test class constructor, use the “setFixture” method.

package com.aqua.services.fixtures;

import junit.framework.SystemTestCase;

public class FixturesDemonstrationTest extends SystemTestCase {
   public FixturesDemonstrationTest(){
       setFixture(ExampleFixture.class);
   }
   
   public void testFixturesExample(){
       report.step("------------------ in test");  
   }
   
   public void testAnotherFixturesExample(){
       report.step("------------------ in test");  
   }
}

When automatically associating a test class with a fixture all of the test cases in the class are associated with the fixture. This behavior can be overridden by associating a fixture to a scenario.

Fixture Navigation

This section describes how the system navigates between fixtures. The following illustration shows the process flow and structure of the fixture tree.

Test and Fixture Association

The table shows the relationship between the tests and their corresponding fixtures. The explanation that follows explains the relationship between the corresponding tests and scenario fixtures.

Test Fixture Description
(Fixture 2) The JRunner executes the scenario T1 (Root) and searches for its associated fixture. After determining that T1 is associated to the fixture F2, it navigates to F2 via F1. The navigation includes running the setup method of F1 and setup method of F2.
(Test 2) (Fixture 3) After navigating to F2 the fixture navigator activates the test T1. After finishing T1 it checks which fixture T2 it is associated to, the fixture is F3 so it navigates from F2 (the current fixture) to F3. This activation includes activating the setup method of F3.
(Test 3) (Fixture 4) After running the T2 test the fixture navigator moves to T3. The fixture which is associated with T3 is F4 so now the fixture navigator has to navigate from F3 to F4. The navigation includes moving from F3 to F2 and from F2 to F1 and from F1 to F4. So to move from F3 to F2 is activates the tearDown method of F3 then the tearDown method of F2, it does not activate the tearDown of F1 but goes to setup method of F4.

Tear Down Fixture

In the event of a “test failure” where the programmer needs to perform a “cleanup” process that differentiates itself from a standard “failtest” process the following teardown fixture is applied, as appears in the code example.

Fail Tear Down Code Example

If the user defines a tear-down fixture for a test and the test fails, the fixtures navigator navigates to the tear down fixture. When navigating to a tear down fixture, the fixture navigator looks for a “failTearDown” method in the fixture.

package com.aqua.services.fixtures;
import jsystem.framework.fixture.Fixture;
public class ExampleFixture extends Fixture {

    public ExampleFixture() {
        setParentFixture(ParentExampleFixture.class);
    }
    
    public void setUp() throws Exception {
        report.step(" in example fixture setup");
    }
    
    public void tearDown() throws Exception {
        report.step(" in example fixture tearDown");
    }
    
    public void failTearDown() throws Exception {
    }
}

Note: In order to support a “failTearDown” method it needs to be added to the fixture.

Test Constructor Code Example

In the test the constructor defines the fail down fixture.

package com.aqua.services.fixtures;

import jsystem.framework.fixture.RootFixture;
import junit.framework.SystemTestCase;

public class FixturesDemonstrationTest extends SystemTestCase {
    public FixturesDemonstrationTest(){
        setFixture(ExampleFixture.class);
        setTearDownFixture(RootFixture.class);
    }
    
    public void testFixturesExample(){
        report.step("------------------ in test");  
    }
 }

Navigating with a TearDown Fixture

Enhancing the fixtures navigation example in order to see how it works with a fail down fixture.

Test Fixture Tear Down Fixture
T1 F2
T2 F3 F1
T3 F4

In the example the teardown fixture of T2 has been defined as F1. During the scenario execution in the event that T2 fails the fixture navigator will then navigate from F3 to F1 in the following way:

  1. The fixture will activate the “failTeardown” of F3, and then it will activate the “failTearDown” of F2.

  2. Before execution the T3 fixture will navigate to the F4 fixture by activating the setup method of the F4 fixture.

Working with Fixtures in the JRunner

The Fixtures tab in the JRunner shows the fixtures that are defined in the system and their resulting hierarchical structure:

During the execution of a scenario, the JRunner graphically navigates down the fixtures tree inside the fixtures tab. The current fixture is highlighted in blue.

Manual Navigation to a Fixture

In order to manually navigate to a fixture press the “Go to” button. To navigate through to the “failTearDown” methods, press the “Fail to” method as appears in the JRunner image.

Setting the Current Fixture without Navigation

In order to manually set the current fixture without having to navigate to it, select the fixture and press the “set current” button.

Associating a Fixture with a Scenario

In addition to associating a fixture to a test programmatically, the JRunner user can associate a fixture with a scenario: The illustrations below demonstrate the steps required to perform the fixture association.

  1. Select the fixture from the “Test Tree” tab.

  1. Press the “add tests” button in order to add the fixture to the scenario.

Note: When associating a fixture with a scenario the fixture is associated with all the tests under it. Associating fixture to a scenario using the JRunner overrides programmatic association of fixture to a test case.

Clone this wiki locally