Effacer les filtres
Effacer les filtres

How can I set a unit test class variable from a test function?

17 vues (au cours des 30 derniers jours)
mg
mg le 31 Mai 2017
Modifié(e) : per isakson le 5 Juin 2017
I am trying to write a unit test class that includes a teardown function that writes a simple report. During execution of the different tests, I'd like to set some class variables that can then be accessed by the teardown function. I've created a simple example below to illustrate this. In this example, why is it that the test01 function can set testCase.variable, but the teardown function doesn't see it?
classdef testClass < matlab.unittest.TestCase
properties
variable = []
end
methods (TestClassTeardown)
function teardown(testCase)
fprintf('Variable was set to %i', testCase.variable);
end
end
methods(Test)
function test01(testCase)
testCase.variable = 5;
testCase.verifyEqual(testCase.variable, 5);
end
end
end

Réponse acceptée

Andy Campbell
Andy Campbell le 31 Mai 2017
Modifié(e) : Andy Campbell le 31 Mai 2017
Hello mg,
In order to support the principle of Independent Tests, each test gets its own independent copy of the TestCase instance. The TestCase instance that is created and passed into TestClassSetup gets copied for every test method. This means that each test can have its own listeners and is more likely to remain independent. However, a shallow copy is made from the instance passed into TestClassSetup and TestClassTeardown methods in order for the test methods to use the data setup in TestClassSetup. But the Test methods are prevented from modifying the Class instance for every other Test method, or for the original TestCase modified in TestClassSetup and subsequently passed into TestClassTeardown.
However, what you are trying to do seems to be best solved by writing a plugin rather than doing this from the test content itself. See the following to get started:
Does that help get you started?

Plus de réponses (0)

Catégories

En savoir plus sur Extend Testing Frameworks dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by