Create a fixture that can be reused for multiple tests
Understand fixtures
Run tests on a folder.
Starting code
Start tag: lesson/10-testing-only
Steps
Now we are in lesson 6 we are going to discover how to reuse previously written steps writing them once and calling from each of the scenarios using fixtures.
The purpose of a test fixture is to ensure that there is a well known and fixed environment in which tests are run so that results are repeatable. Some people call this the test context. Basically a reusable test that we can call before we start any tests
Fixture description
Scenario: Register a superhero via fixture
Scenario description
We are going to create 5 scenarios for this lesson in 5 different files.
Scenario: Register a superhero 1 and Search
Scenario: Register a superhero 2 and Search
Scenario: Register a superhero 3 and Search
Scenario: Register a superhero 4 and Search
Scenario: Register a superhero 5 and Search
Fixtures
Scenario Description: The scenario name must be short and descriptive of what needs to be achieved.
Scenario:To Register a superhero via fixture
Actions: We are going to concentrate on create a fixture that register a super heroe with a random name and can be searched from the scenario.
I execute feature classpath:/<location of the feature>
Before start the test we want to add a new fixture file to the project.. The steps to add a new fixture are:
Right click over common folder > New > Resource
2. Select test file and set as a path /tests/hercules/fixtures add as a name RegisterSuperHero
3. Inside of the folder created we are going to add some documentation for our fixtures. Add a new resource with type ‘Markdown File’ and File name ‘fixtures’
# Folder to store fixtures that will be reused for multiple services
* Registration
* Maintenance
* Etc
Once the text is entered on md file and click on the icon documentation will show something like this.
Inside of the fixture we are going to create the following code (Please note that we are not entering any user detail or setting up the application at top):
Feature: common/tests/hercules/fixtures/RegisterSuperHero
Scenario: Register a superhero via fixture
And I click menu Online Services
And I click menu Register a Super Hero
And I enter text ${superheroname} in Super Hero Name
When I click button Add Secret Identity
Then I enter text Clark in Secret Identity > First name
And I enter text Test in Secret Identity > Middle name(s)
And I enter text Verne in Secret Identity > Last name
And I enter text ${year} in Year of Arrival on Earth
And I select radio button Yes in radio button group Do you have a uniform, costume or outfit?
And I select radio button Yes in radio button group Do you usually or intend to don a cape?
And I select option ${power} from dropdown Hero Powers
And I click button Submit
Then I expect no invalid config
And I expect service-title Success
And I expect text Your super hero is now registered and ready to save the world!
And I expect text Super Hero Name > ${superheroname}
And I expect text Registration Date > #FORMATTED_TODAY#dd MMMM yyyy#FORMATTED_TODAY#
And I expect text Year of Arrival on Earth > ${year}
And I expect text Uniform/costume/outfit? > Yes
And I expect text Including a cape? > Yes
And I expect text Hero Powers > ${power}
When I click button Back
Fixtures that are located in common can be reused by other services. If the fixture is saved in other locations will be accessed only at the application level. For example, a fixture located in heroes will be accessible only from heroes and not from tests located in vehicles
At this point the fixture will only concentrate on fill the form with variables that we are going to pass from the main test.
We are going to create lesson 6 folder under heroes with name H006 and file name Test1 and we are going to repeat the same process to create Test2, Test3, Test4, Test5
Please note this test used a login for External01 and for each different test we are going to change the user for External02, External03, External04, External05.In the example below you are going to find the execute feature step which contains a path. To obtain the path please copy the relative path from the fixture.
After we copy the path we can use the path in our test example replacing the Execute feature step with the classpath from the Fixture
Feature: heroes/tests/hercules/tests/H006/Test1
Scenario: Register and Search superhero 1
Given I set application to heroes
Then I logon as External01@fostermoore.com with password External01
And I create a random 10 titlecase character variable called superheroname
And I create a variable called year with value 1980
And I create a variable called power with value Flight
When I execute feature classpath:common/tests/hercules/fixtures/RegisterSuperHero.feature
And I click menu Online Services
And I click menu Search Super Heroes
Then I expect service-title Search Super Heroes
When I enter text ${superheroname} in Name
And I click button Search
Then I click link ${superheroname}
And I expect no invalid config
And I do not expect text Your super hero is now registered and ready to save the world!
And I expect text Super Hero Name > ${superheroname}
And I expect text Registration Date > #FORMATTED_TODAY#dd MMMM yyyy#FORMATTED_TODAY#
And I expect text Year of Arrival on Earth > ${year}
And I expect text Uniform/costume/outfit? > Yes
And I expect text Including a cape? > Yes
And I expect text Hero Powers > ${power}
And I click button Filings
Navigating fixtures
If for some reason we want to navigate to the fixture we can always access the fixture by pressing control + click on the fixture path. That way Magellan will open the fixture directly
Variables on features and fixtures
All variables are accessible through the icon Test Run Variables. All variables can be call by ${name of the variable}
Running tests on a folder
Once we create our 5 tests we can run all the tests together using the Run Test(s) – Current folder only. The Run Test(s) only option it’s useful when there are nested folders under 1 folder.