How to use parameterized TestCase
3 views (last 30 days)
Show older comments
Dear all,
I use the matlab unittest framwork and have the following problem/question. Some of my tests shall access a DUT (device under test) which is connected using TCP. Thus, the tests needs to know the IP Address of the DUT. For me, it is not clear how to solve this issue. I read about ClassSetupParameter and TestClassSetup without success.
One of my test classes looks like:
classdef A_Tests < matlab.unittest.TestCase
methods(Test)
function test_1(testCase)
(do stuff)
end
end
end
I use a m-file to define my tests like:
import matlab.unittest.TestRunner;
import matlab.unittest.TestSuite;
...
dut_ip = '127.0.0.1';
...
A_Suite = TestSuite.fromClass(?A_Tests);
B_Suite = TestSuite.fromClass(?B_Tests);
RegressionSuite = [A_Suite B_Suite];
runner = TestRunner.withTextOutput;
result = runner.run(RegressionSuite);
The question is how to access the dut_ip variable inside A_Tests, or how to use dut_ip as an parameter for A_Tests.
Thanks a lot in advance.
Best regards, Nico
3 Comments
Andy Campbell
on 30 Jun 2015
What do you mean with "programmatically access this IP address"?
I was wondering if you make a call to connect to the DUT and it returns the IP address for that connection. Then you could just call that specific function to connect and use the IP from that. Do these IP addresses change frequently? Are they different when the test is run from different machines or environments?
Do you want to run the same suite across many DUTs during the same test run? How are these IP addresses assigned and is there any way to call a function or ask some other piece of software for these IPs?
Do you have an API available to you (or could you make one) to tie a device name to the IP like the following?
ip1 = DeviceIP.getIPFor('device1');
ip2 = DeviceIP.getIPFor('device2');
Answers (1)
Sean de Wolski
on 30 Jun 2015
Edited: Sean de Wolski
on 30 Jun 2015
Set it up in a (TestClassSetup) method (so it gets set up before any tests run) and store it as an object property for the remainder of the methods to use. Then close the connection in a (TestClassTeardown) method.
It's not a parameter in the test case sense where there is an enumerated list of parameters you want to sweep over, it's just a property that the tests need to know about.
Here's a small example:
classdef extcp < matlab.unittest.TestCase
properties
Connection
end
methods (TestClassSetup)
function openConnection(testCase)
testCase.Connection = open(whatever);
end
end
methods (TestClassTeardown)
function closeConnection(testCase)
close(testCase.Connection)
end
end
methods (Test)
function testSomething(testCase)
use(testCase.Connection)
end
end
end
3 Comments
Sean de Wolski
on 30 Jun 2015
At the beginning of each test or before all tests? If before each test there's also the (TestMethodSetup) attribute.
See Also
Categories
Find more on Function-Based Unit Tests in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!