【问题标题】:Matlab: Create workspace variables in unit testingMatlab:在单元测试中创建工作区变量
【发布时间】:2017-07-24 09:03:01
【问题描述】:

我有一个类,它从工作区读取所有变量并获取所有传递函数的列表(类类型tf),并将它们存储在另一个类中(此处未表示)。

classdef TransferFunctionFactory < handle
  % This class allows to import a series of transfer function loaded from
  % different sources, like a file folder, workspace and so on..

  % Public section
  methods(Access = public)

    function collection = fromWorkspace(this)
      % Create a collection of transfer function that are stored in the workspace.
      % Returns:
      %  Collection of transfer functions.
      collection = App.TransferFunctionCollection();
      variables = who; % Here I should see w1, w2, w3, and w4 in variables
      for i = 1 : length(variables)
        if isa(eval(variables{i}), 'tf')
          trf = App.TransferFunction();
          trf.setAttribute('Name', variables{i});
          trf.setTransferFunction(eval(variables{i}));
          collection.addTransferFunction(trf);
        end
      end

    end
  end

我想进行单元测试,所以我创建了一个测试类:

classdef TransferFunctionFactory < matlab.unittest.TestCase

  methods (Test)

    function loadControllersFromWorkspace(testCase)
      collection = factory.fromWorkspace();
      testCase.verifyGreaterThanOrEqual(collection.getSize(), 3);
    end

  end

end

为了执行单元测试,我需要在工作区中设置一些变量,以便从函数中进行分析。我试过类似的东西:

function loadControllersFromWorkspace(testCase)
  factory = App.TransferFunctionFactory();
  assignin('base', 'w1', tf(1 + 's'));
  assignin('base', 'w2', tf(3 / 's'));
  assignin('base', 'w3', 4.3);
  assignin('base', 'w4', tf(1 / ('s' + 1)));
  who
  collection = factory.fromWorkspace();
  testCase.verifyGreaterThanOrEqual(collection.getSize(), 3);
end

但它不起作用。当我输入factory.fromWorkspace 方法时,我看不到w1w2w3w4 作为who 的输出,我无法使用它们。

如何在单元测试类方法的工作区中设置变量,以便在测试内部调用的函数/方法中可以看到它们?

【问题讨论】:

  • 什么是's' 在您的最终代码块中,以及您在assignin 调用期间试图将什么传递给tf
  • s 是我定义传递函数所需的东西,它不是变量
  • 但是你知道1 + 's' = 116吗?因为给一个字符加一个值会导致这个字符被当作它的字符值...
  • 对,但在该文档中没有任何地方传递一个整数值,该整数值是通过将字符添加到数字而创建的。您将's' 指定为连续模型的参数,作为字符串。如果您正在做的事情适合您的目的,那没关系,但我认为您想要 tf(3, [1 0]) 而不是 tf(3 / 's') 将被解释为 tf(3/115) = tf(0.0261)

标签: matlab unit-testing workspace


【解决方案1】:

我已经通过使用evalin 解决了。在我的方法中,我使用它来在需要时从基础工作区读取变量:

function collection = fromWorkspace(this)
      collection = App.TransferFunctionCollection();
      variables = evalin('base', 'who');
      disp(variables);
      for i = 1 : length(variables)
        command = ['isa(', variables{i}, ', ''tf'')'];
        if evalin('base',command);
          trf = App.TransferFunction();
          trf.setTransferFunction(evalin('base', variables{i}));
          collection.addTransferFunction(trf);
        end
      end
    end

当我需要在工作区中保存变量时,我也在测试方法中使用过它:

function loadControllersFromWorkspace(testCase)
      factory = App.TransferFunctionFactory();
      evalin('base', 'w1 = tf(1 + ''s'')');
      evalin('base', 'w2 = tf(3 / ''s'')');
      evalin('base', 'w3 = 4.3');
      evalin('base', 'w4 = tf(1 / (''s'' + 1))');
      %who
      collection = factory.fromWorkspace();
      testCase.verifyGreaterThanOrEqual(collection.getSize(), 3);
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-29
    • 1970-01-01
    • 2019-09-22
    • 2012-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多