Main Content

Simulink Exercise Assessment Tests in MATLAB Course Designer

Assessment tests let you automatically test learner solutions to Simulink® exercises in MATLAB® Course Designer™.

Assessment with one passed and one failed test

For an example of how to create a Simulink exercise, see Create Simulink Exercise Using MATLAB Course Designer.

Open Task Solution

Open your course in MATLAB Course Designer. Navigate to the exercise in which you want to add assessment tests and click Edit.

To open the solution for the task in which you want to add assessment tests:

  1. In the task pane, click the task.

  2. Add a solution if you have not already done so. To add a solution, see Add Task Solution.

  3. If the solution is open, click the Solution tab. Otherwise, on the Edit Task tab, in the left navigation pane, click Solution. Then, next to the solution filename, click Open.

The solution includes an Assessment pane that helps you add tests.

Assessment pane with Add Test button

Add Tests with Dependencies on Block Diagrams

To test elements such as blocks and connections, you must select the elements and then add the tests. The elements can be in the solution model or its subsystems.

Animation of Add Test list based on block diagram selections. The pointer expands the Add Test list, which has Block existence, Block parameter value, and Signal line dimmed. Model parameter value, Signal data, and Custom are enabled. The pointer selects a block and the Block existence and Block parameter value are enabled. The pointer selects a signal line and the block options are dimmed while Signal line is enabled. The pointer selects a block and clicks Block existence in the Add Test list. A block existence test is created for the selected block.

As you navigate the model hierarchy, only the tests for the current block diagram are enabled for editing. The tests for other block diagrams are dimmed.

To edit a test for another block diagram, click the test to open the corresponding block diagram.

To highlight the blocks or signal lines that correspond with a test, click the test.

Animation of test and block diagram interactions. The pointer clicks a block existence test and the corresponding block is highlighted. The pointer clicks a dimmed signal line test and the corresponding subsystem opens. The pointer clicks the signal line test again and the corresponding source block, signal line, and destination blocks are highlighted. The pointer clicks the now-dimmed block existence test and the block diagram returns to the model root.

Add Test for Block Existence

To test that the learner solution contains the specified blocks:

  1. In the current block diagram, select one or more blocks.

  2. In the Assessment pane, select Add Test > Block existence. A new test appears for each selected block.

Block existence test with corresponding Step block highlighted in block diagram

Learners receive a question about whether the model contains a specific type of block, such as a Step block.

Learner preview of block existence test

When you test the existence of more than one block of the same type, a parenthetical provides the number of expected blocks of that type.

Learner preview of block existence test with parenthetical

Add Test for Block Parameter Value

To test that block parameter values match what you specify:

  1. In the current block diagram, select one or more blocks.

  2. In the Assessment pane, select Add Test > Block parameter value. A new test appears for each selected block.

  3. In the Parameter list, select the block parameter to test. Select only documented block parameters. Undocumented block parameters are for internal use only.

  4. Check that the parameter value matches expectations.

  5. To test additional parameter values for the same block, click Add Parameter. For each addition, select the block parameter to test and check the parameter value.

If you update the value of a block parameter, the test automatically updates to reflect the new value.

Block parameter value test with corresponding Step block highlighted in block diagram

Learners receive a question about whether the block has the correct parameter settings.

Learner preview of block parameter value test

Add Test for Signal Line

To test that signal lines connect blocks as expected:

  1. In the block diagram, select one or more signal lines.

  2. In the Assessment pane, select Add Test > Signal line. A new test appears for each selected signal line.

  3. If a signal branches to more than one destination, optionally, test that the signal connects to additional destinations. For each destination that you want to test, click Add Destination and select the block port that receives the signal.

Block connection test for the signal line between the Step block and Sum1 block

Learners receive a question about whether a signal line connects the blocks.

Learner view of block connection test

Add Test for Model Parameter Value

To test that the value of a model parameter matches what you specify:

  1. Select Add Test > Model parameter value.

  2. In the Parameter list, select the model parameter to test.

  3. Check that the nested parameter value matches expectations.

  4. To test additional model parameter values, click Add Parameter. For each addition, select the model parameter to test and check the parameter value.

If you update the value of a model parameter, the test automatically updates to reflect the new value.

Model parameter value test for the Stop time and Solver model configuration parameters

Learners receive questions about whether the selected model parameters have the correct settings.

Learner preview of configuration parameter value test

Add Test for Signal Data

To test that the learner solution generates signal data that matches the expected signal data within the specified tolerance:

  1. Select Add Test > Signal data. This action adds a Signal Assessment block.

  2. Connect a signal to the Signal Assessment block.

  3. Click Capture Signal Data. The Signal source value updates to indicate the source block.

  4. To check the captured signal data in a figure window, click Signal data captured.

  5. Optionally, specify an absolute tolerance in the Tolerance box. When the learner solution uses a different approach than the reference solution, the tolerance can determine whether the test passes. For each time step, the test checks whether the learner’s signal data differs from the expected value by more than the specified absolute tolerance.

  6. If you update the model, recapture the signal data. The signal data you capture is a snapshot that does not automatically update when you update the model.

Signal data test with Signal Assessment block connected in block diagram

Learners receive a plot of the signal requirement and a question about whether the signal meets the requirement.

Learner preview of signal data test

For each task, you can test the data of only one signal.

Add Custom Test

To write a custom test using a MATLAB function:

  1. Select Add Test > Custom.

  2. In the Question box, enter the question for the learner.

  3. Next to Function, click the hyperlink. The function opens in a Custom Test tab. When you have more than one custom test, the tab names include numbers for uniqueness.

  4. In the function, implement custom logic that tests the learner solution.

    • The function can use the input argument mdl for the name of the solution model in quotes, such as "SolutionModel".

    • The function must have an input argument, even if the input argument is unused.

    • The function must return whether the learner solution passes the test as numeric or logical 1 (true) or 0 (false).

  5. To check whether a custom test is free of errors, click Run Test.

  6. Check whether the test is valid and whether your solution passes the test.

For example, suppose your solution has a PID Controller block named Cruise Control with one input signal, and you want to check that learners name the input signal error. The custom test could be as follows.

function pass = customTest(mdl)
    ports = get_param(mdl+"/Cruise Control","PortHandles");
    in1 = ports.Inport(1);
    signal = get_param(in1,"Name");
    pass = (signal == "error");
end

After you run the test, you receive a message that says whether your solution passes the test.

Solution passes custom test in Assessment pane

Learners see only the question that you author. They do not see the function.

Learner preview of custom test

For another example, suppose you want to check that learners add to the base workspace a variable named k with a value of 5. The custom test could be as follows.

function pass = customTest(mdl)
    k = evalin('base','k');
    pass = (k == 5);
end

If you set k to 5 in the base workspace and then run the test, you receive a message that your solution passes the test.

Solution passes custom test in Assessment pane

Learners see the question that you author.

Learner preview of custom test

See Also

Topics