Test if structure array has specified field
matlab.unittest.constraints.HasField class provides a constraint to
test if a value is a structure array that has a specified field.
Field — Name of expected field
Name of the expected field, returned as a character vector. Specify the value of this property during creation of the constraint as a string scalar or character vector.
Test If Structure Has Specified Field
Test a structure using the
First, import the classes used in this example.
import matlab.unittest.TestCase import matlab.unittest.constraints.HasField
Create a test case for interactive testing.
testCase = TestCase.forInteractiveUse;
Create a structure with two fields.
s = struct("ID",10,"score",90);
s is a structure with an
Test if the structure has a
"Score" field. The test fails because
the field name comparison is case sensitive.
Verification failed. --------------------- Framework Diagnostic: --------------------- HasField failed. --> The value did not have the expected field. Actual Fieldnames: 'ID' 'score' Expected Fieldname: 'Score' Actual Value: struct with fields: ID: 10 score: 90
Test if the structure has both an
"ID" and a
"score" field. The test passes.
testCase.verifyThat(s,HasField("ID") & HasField("score"))
Verify that the structure does not have a
Introduced in R2013b