Pass parameter to class based unit test
Ältere Kommentare anzeigen
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.
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Create and Run MATLAB Tests finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!