Main Content

Generate Test Cases for Embedded Coder Generated Code

When you use Embedded Coder® to generate code from a model set to software-in-the-loop (SIL) mode, you can use Simulink® Coverage™ to record coverage metrics on the generated code. However, the same tests that enable you to achieve 100% model coverage might not produce 100% coverage for the generated code. Some differences between the output code and the model can cause gaps in the code coverage compared to the model coverage:

  • Extra custom code files

  • Shared utility files

  • Code transformations, such as:

    • Expression folding

    • Simplified or expanded expressions

    • New decision points due to lookup tables

You can use Simulink Design Verifier™ to generate test cases to increase coverage for generate code. You generate test cases for generated code from the block diagram, by using the Simulink Design Verifier API, or from the Simulink Test™ Test Manager. Before you generate test cases, you need to record coverage results at least once.

Generate Test Cases for Generated Code from the Simulink Model Toolstrip

After you Enable SIL Code Coverage for a Model (Simulink Coverage), simulate the model, and record code coverage data, you use Simulink Design Verifier to generate additional test cases for the generated code:

  1. On the Design Verifier tab, in the Mode section, select Test Generation.

    • To generate tests for code generated as top model, select Target > Code Generated as Top Model, then click Generate Tests.

    • To generate tests for code generated as model reference, select Target > Code Generated as Model Reference, then click Generate Tests.

    Simulink Design Verifier test generation proceeds according to the test generation mode that you choose.

To learn more about the differences between code generated as top model and code generated as model reference, see:

Generate Test Cases for Generated Code by Using the Simulink Design Verifier API

For an example of how to programmatically generate test cases for generated code, see Code Coverage Test Generation.

Generate Test Cases for Generated Code from the Simulink Test Test Manager

If you use the Simulink Test Test Manager to record code coverage for a model set to SIL mode, you can incrementally increase coverage for the generated code directly from the Test Manager. For more information, see Incrementally Increase Test Coverage Using Test Case Generation.

Related Topics