Pass parameter to class based unit test

11 visualizzazioni (ultimi 30 giorni)
I have written matlab code to interface with a hardware device through tcp/ip.
I have written a class based testcase for it, but I would like to be able to select the ip-address where the device is residing whenever I want to run my tests. I also want to be able to selectively run tests from my testcase class. To do this, I want to be able to create a testsuite from the testcase, and only run selected indices from the generated testsuite array.
To be able to specify the ip-address, I added a constructor parameter to my test case to set a property, which is then used to open the device at the correct location. This works fine to run the complete testcase without explicit testsuite, but I cannot generate a testsuite from it.
Is there a way to do this more elegantly, giving me the options to pass the parameter when I want to test, and still have control over which tests I want to run ?
(Note that this has nothing to do with parametric testing)
Concept code for the testcase definition:
% My testcase definition
classdef tc < matlab.unittest.TestCase
properties
host_; % Ip address of the device
end
methods
% constructor to allow specifying correct address
function self = tc(host)
self.host_ = host;
end
end
% test fixtures and test methods using self.host_ to open device
end
This allows to run the full testcase specifying the host parameter, giving me test results for the 72 (parametric) tests in the testcase class
run(tc('192.168.1.5')) % run all tests with the device at this ip-address
I can create a testsuite from the class, but when I run tests from the suite it will throw an error because the testcase constructor argument is missing.
suite = matlab.unittest.TestSuite.fromClass(?tc)
run(suite(1)) % causes missing input argument error
Is there a way to create the testsuite from a class object rather than from the class, so that I can specify the constructor argument and still have control over which exact tests I want to run.

Risposta accettata

Steven Lord
Steven Lord il 16 Mar 2022
Rather than giving your test class a constructor I'd specify the parameter as a ClassSetupParameter (with some reasonable default) and use the capability to specify an external parameter value.
classdef test1673009 < matlab.unittest.TestCase
properties(ClassSetupParameter)
identifier = struct('id', 1);
end
methods(TestClassSetup)
function reportID(~, identifier)
fprintf("Running test with ID %d\n", identifier);
end
end
methods(Test)
function basicTestCase(testcase, identifier)
testcase.verifyGreaterThan(identifier, 0);
end
end
end
If I run the test without specifying a value for the parameter it uses the default given in the test.
>> suite = matlab.unittest.TestSuite.fromClass(?test1673009);
>> run(suite)
Running test1673009
Running test with ID 1
.
Done test1673009
But I can inject a parameter when I create the suite.
>> newparam = struct('id', 12345);
>> P = matlab.unittest.parameters.Parameter.fromData('identifier', newparam);
>> suite = matlab.unittest.TestSuite.fromClass(?test1673009, ...
'ExternalParameters', P);
>> run(suite)
Running test1673009
Running test with ID 12345
.
Done test1673009

Più risposte (0)

Categorie

Scopri di più su Create and Run MATLAB Tests in Help Center e File Exchange

Prodotti


Release

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by