zam_test is a typed version of the test package.
Includes the following core components.
Check out all the components in detail here
Test
is the main class. It has two constructor methods, theTest.single
and theTest.multi
. If your test is simple and has only one case, then useTest.single
.TestCase
is something that is wrapped inside aTest.multiple
. Avoid using aTestCase
directly.TestGroup
is used to run multipleTest
together.
Test
can be seen as the typed version of test()
function in the original test package. Mostly a Test
will have multiple cases based on different inputs. For example, a login test has multiple test cases based on different inputs like wrong username, wrong password, correct username and password, etc. A Test
can also have a single test case. Therefore, we have MultiCasedTest
and SingleCasedTest
respectively to address these.
You can also create a new test by extending Test
by following the steps given below.
- Create a class extending
Test
. - Provide a
name
. - Override
run
function which is called for everyTestCase
. - Provide a list of
cases
. - Override
intialize
anddispose
when required. - You can customize the
nameSuffix
and thedescription
too.
class HeightTest extends Test<double, String> {
@override
final name = 'Height';
@override
run(input) {
return Height(input).toStringInMetre();
}
@override
final cases = [
NegativeTestCase(
when: 'Negative height value',
input: -23,
exception: HeightNotValidException,
),
NegativeTestCase(
when: 'Zero height value',
input: 0,
exception: HeightNotValidException,
),
ValueTestCase(
when: 'Positive Border height value',
then: 'outputs value in m',
input: 1,
output: '0.01 m',
),
];
}
void main() {
HeightTest().execute();
}
TestGroup
is used to run multiple Test
together. It is more of a utility class. You can run tests without this.
void main() {
TestGroup('BMI', [
HeightTest(),
// WeightTest(),
// HeightTest(),
// BmiCategoryTest(),
// ...
// ...
// ... (you can add more test groups here)
]).execute();
}
It accepts two descriptive texts when
and then
, an input
, a matcher
and an optional callback called the action
.
CONSIDER: Always consider executing it inside a Test
class even when you want to run a single test case which is accomplished using Test.single
constructor.
Simple way to execute a TestCase
is as follows.
void main() {
ValueTestCase(
when: 'Positive Border height value',
then: 'outputs value in m',
input: 1.0,
output: '0.01 m',
action: (double input) => Height(input).toStringInMetre(),
).execute();
}
Currently we have provided the following test case types which derive from TestCase
. We expect this list to grow in the future to handle various scenarios. Until then you can use TestCase
since it accepts a custom matcher.
- ValueTestCase - To match direct values.
- NegativeTestCase - To match exceptions.
- BooleanTestCase - To match booleans.
To learn more, move on to the example section or check out these dedicated examples in github.
You can override the following at the moment.
TestCase
->descriptionDelimiter
- Defaults to' -> '
.TestCase
-> description - It is generated by combining thewhen
andthen
texts with adescriptionDelimiter
in between.Test
->nameSuffix
- Defaults to':'
.Test
->description
- It is generated by combiningname
andnameSuffix
.TestGroup
->nameSuffix
- Defaults to' -'
.TestGroup
->description
- It is generated by combiningname
andnameSuffix
.