Writing Tests in Inferno
Test Suite Structure
There are three classes used to organize tests in Inferno:
TestSuite
- An entire suite of tests. ATestSuite
can contain one or moreTestGroup
classes.TestGroup
- ATestGroup
can contain one or moreTestGroup
orTest
classes.Test
- An individual test. ATest
contains arun
block which defines what happens when the test is run.
In the Inferno DSL, each is considered a Runnable.
For example, a simple US Core test suite might look like this:
- US Core (
TestSuite
)- US Core Patient Group (
TestGroup
)- Server supports Patient Read Interaction (
Test
) - Server supports Patient Search by id (
Test
)
- Server supports Patient Read Interaction (
- US Core Condition Group (
TestGroup
)- Server supports Condition Read Interaction (
Test
) - Server supports Condition Search by Patient (
Test
)
- Server supports Condition Read Interaction (
- US Core Patient Group (
Defining Groups and Tests
Let’s show how to add groups and tests to a TestSuite
using the above US Core example.
To start, we define the groups in the TestSuite
using the group
method.
# lib/us_core_test_kit.rb
module USCoreTestKit
class USCoreTestSuite < Inferno::TestSuite
group do
title 'US Core Patient Group'
end
group do
title 'US Core Condition Group'
end
end
end
We can then add the tests to each group using the test
method:
# lib/us_core_test_kit.rb
module USCoreTestKit
class USCoreTestSuite < Inferno::TestSuite
group do
title 'US Core Patient Group'
test do
title 'Server supports Patient Read Interaction'
input :patient_id
run do
# test code goes here
end
end
test do
title 'Server supports Patient Search by id'
input :patient_id
run do
# test code goes here
end
end
end
group do
title 'US Core Condition Group'
test do
title 'Server supports Condition Read Interaction'
input :condition_id
run do
# test code goes here
end
end
test do
title 'Server supports Condition Search by Patient'
input :patient_id
run do
# test code goes here
end
end
end
end
end
This test suite is already getting pretty long. We can improve the organization using externally defined groups and tests.
Externally Defined Groups and Tests
Let’s move the Patient and Condition groups into their own files, and assign them ids.
# File: lib/us_core_test_kit/us_core_patient_group.rb
module USCoreTestKit
class USCorePatientGroup < Inferno::TestGroup
title 'US Core Patient Group'
id :us_core_patient
test do
title 'Server supports Patient Read Interaction'
input :patient_id
run do
# test code goes here
end
end
test do
title 'Server supports Patient Search by id'
input :patient_id
run do
# test code goes here
end
end
end
end
# File: lib/us_core_test_kit/us_core_condition_group.rb
module USCoreTestKit
class USCoreConditionGroup < Inferno::TestGroup
title 'US Core Condition Group'
id :us_core_condition
test do
title 'Server supports Condition Read Interaction'
input :condition_id
run do
# test code goes here
end
end
test do
title 'Server supports Condition Search by Patient'
input :patient_id
run do
# test code goes here
end
end
end
end
Now the suite can include these groups without having to contain their entire definitions.
# File: lib/us_core_test_kit.rb
require_relative 'us_core_test_kit/us_core_patient_group'
require_relative 'us_core_test_kit/us_core_condition_group'
module USCoreTestKit
class USCoreTestSuite < Inferno::TestSuite
group from: :us_core_patient
group from: :us_core_condition
end
end
We can take it a step further and move the tests into their own files. This is often beneficial because it encourages decoupling tests from each other, and allows for more dynamic test group composition.
# File: lib/us_core_test_kit/us_core_patient_read_test.rb
module USCoreTestKit
class USCorePatientReadTest < Inferno::Test
title 'Server supports Patient Read Interaction'
id :us_core_patient_read
input :patient_id
run do
# test code goes here
end
end
end
# File: lib/us_core_test_kit/us_core_patient_search_by_id_test.rb
module USCoreTestKit
class USCorePatientSearchByIdTest < Inferno::TestGroup
title 'Server supports Patient Search by id'
id :us_core_patient_search_by_id
input :patient_id
run do
# test code goes here
end
end
end
And then add each Test
to their respective TestGroup
.
# lib/us_core_test_kit/us_core_patient_group.rb
require_relative 'us_core_patient_read_test'
require_relative 'us_core_patient_search_by_id_test'
module USCoreTestKit
class USCorePatientGroup < Inferno::TestGroup
title 'US Core Patient Group'
id :us_core_patient
test from: :us_core_patient_read
test from :us_core_patient_search_by_id
end
end
Note: When importing a group, its optional children can be omitted:
group from: :us_core_patient, exclude_optional: true
Suggest an improvement
Want to make an change? Contribute an edit for this page on the Inferno Framework GitHub repository.