How to use parameterized TestCase

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 Kommentare

Andy Campbell
Andy Campbell am 30 Jun. 2015
Bearbeitet: Andy Campbell am 30 Jun. 2015
Clarifying question. It sounds like you cannot programmatically access this IP address and it must be hardcoded, is that correct?
If you can access it programmatically somehow (perhaps through creating the connection) then you may be able to do something similar to Sean's answer. However, if the IP is purely manual and not accessible programmatically then we will have to go with one of a couple other strategies.
Nico Falk
Nico Falk am 30 Jun. 2015
The example above is just as an entry point for discussion. I don't want to hardcode the IP-Addr inside my classdef. But to run the tests at the desired DUT, each test suite (e.g. A-Suite and B-Suite in the example above) needs to know which DUT should be used. The IP-Addr shall be set e.g. in the script I call the "runner.run(RegressionSuite)" command (see example above).
What do you mean with "programmatically access this IP address"?
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');

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Sean de Wolski
Sean de Wolski am 30 Jun. 2015
Bearbeitet: Sean de Wolski am 30 Jun. 2015

0 Stimmen

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 Kommentare

Thanks Sean for your quick reply. The documentation listed in your answer describes, how to generate a property or variable which is accessible in all of the tests in this class. There is also an example how to use an environment variable (path) to set up the tests. But I cannot find the solution to set a variable in TestClassSetup with a value defined outside of this scope.
E.g.
classdef A_Tests < matlab.unittest.TestCase
properties
DUT
end
methods(TestClassSetup)
function setup_DUT(testCase)
testCase.DUT = '127.0.0.1';
end
end
methods(Test)
function Test_1(testCase)
testCase.DUT
testCase.verifyEqual(1, 1, 'Equal');
end
end
end
This gives the following output:
Running A_Tests
ans =
127.0.0.1
.
Done A_Tests
Of course, DUT is accessible in all tests. But how to set it to a different value at the beginning of the test?
Thanks, Nico
Sean de Wolski
Sean de Wolski am 30 Jun. 2015
At the beginning of each test or before all tests? If before each test there's also the (TestMethodSetup) attribute.
Nico Falk
Nico Falk am 30 Jun. 2015
Before all tests. All tests shall run on the same DUT.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Verify Generated Code and Deployed Code Artifacts finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 30 Jun. 2015

Kommentiert:

am 30 Jun. 2015

Community Treasure Hunt

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

Start Hunting!

Translated by