Lesson 3 of 5 Kiro and AI-Assisted Development

Generating, Running and Analysing Tests with AI Tools

This lesson explains how AI tools generate example-based and property-based tests, how hooks and AWS CodeBuild run them automatically, and how to keep generated tests trustworthy.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain why tests generated from requirements are more reliable than tests generated from code.
  • Distinguish example-based tests from property-based tests.
  • Describe how CodeBuild publishes test and code coverage reports.
  • Identify the weaknesses of generated tests and the practices that address them.

Generating tests from requirements

Tests are only as good as the behaviour they check. Generating them from a spec ties each test to an agreed acceptance criterion rather than to whatever the code currently does. In a Kiro spec, the task list includes tests for each requirement, and the agent writes them with the team's test framework and mocks, such as moto or botocore Stubber for AWS calls in Python.

For existing code without a spec, the developer can ask the agent to add unit tests for a module, listing the cases that matter: normal input, boundary values, invalid input and each error the code handles. The agent can also read a coverage report and propose tests for the lines and branches that no test reaches.

Property-based tests

An example-based test checks one input against one expected output. A property-based test states a property that must hold for every valid input, such as "the total after a discount is never negative and never greater than the original total", and a library generates hundreds of inputs to try to break it, shrinking any failure to the smallest input that shows the problem. Kiro can derive such properties from a spec's acceptance criteria and generate the tests, which find edge cases that hand-written examples miss.

The following comparison shows when each style of test is most useful.

Example-based test
ChecksOne input against one expected result
FindsRegressions in known cases
Best forDocumenting specific behaviour
Choose when A specific case must keep working.
Property-based test
ChecksA rule that must hold for all generated inputs
FindsUnexpected edge cases, such as empty, very large or unusual values
Best forCalculations, parsers and data transformations
Choose when A rule must hold across a wide range of inputs.
Sort into groups

Example or property?

Exam skills 3.2.5

Decide whether an example-based or a property-based test fits each check.

Log in to try this activity Interactive activities are free with an account.

Automating test runs and analysis

Running tests should require no effort. In the IDE, a PostFileSave hook runs the tests for the changed module, and a Stop hook runs the full suite when the agent finishes a task. When a test fails, the agent reads the output, explains whether the test or the code is wrong and proposes a fix.

In the pipeline, AWS CodeBuild runs the suite on every commit. A reports section in buildspec.yml publishes test reports in formats such as JUnit XML and code coverage reports in formats such as JaCoCo, Cobertura or LCOV, so trends are visible in the console. A failing test returns a non-zero exit code and fails the build, and a script can also fail the build when coverage falls below a threshold. Regression testing is automated by keeping every bug's test in the suite: a bugfix spec records the behaviour that must not change, and its tests run on every build from then on.

Keeping generated tests trustworthy

Generated tests fail in predictable ways. A test written by reading the code may assert the code's current behaviour, including its bugs. Heavy mocking can produce tests that pass whatever the code does. And a model may not think of the edge cases that matter in the business. The following practices address these weaknesses.

  • Review each assertion against the requirement, not against the code.
  • Break the implementation deliberately and confirm that the tests fail (mutation testing automates this).
  • Keep integration tests against real AWS resources in a test account, because mocks cannot detect missing permissions.
  • Track which requirements have tests, as well as line coverage.
Worked example

A discount calculation

A pricing function applies percentage discounts and coupons. A developer asks Kiro for tests.

The agent generates example tests and a property test stating that the result is between zero and the original total. The property test fails at once: a 100% discount combined with a fixed coupon produces a negative total. One generated example test, however, asserted that a 15% discount on 9.99 gives 8.49, which matches the code's rounding but not the requirement to round half up to 8.50.

The developer corrects the example test to match the requirement and fixes both defects. Both tests stay in the suite as regression tests, and CodeBuild runs them on every commit. The example shows both sides of AI-generated tests: they found a real bug, and one of them had encoded another.

Check yourself

How can a developer confirm that a generated test suite would actually catch defects?

Show answer

Break the implementation deliberately, for example by changing a comparison, and check that the tests fail. Mutation testing tools automate this; tests that still pass are not checking the behaviour.

Exam Tips

Remember for the exam

  • Tests that trace to acceptance criteria: generate them from a Kiro spec.
  • Find edge cases automatically: property-based tests.
  • Run tests whenever code changes: Kiro hooks locally and CodeBuild in the pipeline.
  • Publish test and coverage reports in CodeBuild with a reports section in buildspec.yml.
  • A generated test can assert a bug; review assertions against the requirements.