如何将多个参数传递给在 Matlab xUnit 中共享相同设置代码的测试?

发布于 2024-08-05 07:53:24 字数 1042 浏览 13 评论 0原文

根据“如何编写测试共享通用设置代码”是否可以:

function test_suite = testSetupExample
 initTestSuite;

function fh = setup
 fh = figure;

function teardown(fh)
 delete(fh);

function testColormapColumns(fh)
 assertEqual(size(get(fh, 'Colormap'), 2), 3);

function testPointer(fh)
 assertEqual(get(fh, 'Pointer'), 'arrow');

但我无法让它使用更多参数:

function test_suite = testSetupExample
 initTestSuite;

function [fh,fc] = setup
 fh = figure;
 fc = 2;
end

function teardown(fh,fc)
 delete(fh);

function testColormapColumns(fh,fc)
 assertEqual(size(get(fh, 'Colormap'), fc), 3);

function testPointer(fh,fc)
 assertEqual(get(fh, 'Pointer'), 'arrow');

当我运行测试时,它说:

<块引用>

输入参数“fc”未定义。

这是为什么?我做错了什么或者当前版本的 Matlab xUnit 不支持它?如何规避呢?

PS:实际上我的MATLAB要求每个函数都有一个结束。我没有将它们写在这里是为了与手动示例保持一致。

According to "How to Write Tests That Share Common Set-Up Code" is it possible to:

function test_suite = testSetupExample
 initTestSuite;

function fh = setup
 fh = figure;

function teardown(fh)
 delete(fh);

function testColormapColumns(fh)
 assertEqual(size(get(fh, 'Colormap'), 2), 3);

function testPointer(fh)
 assertEqual(get(fh, 'Pointer'), 'arrow');

But I couldn't make it work with more parameters:

function test_suite = testSetupExample
 initTestSuite;

function [fh,fc] = setup
 fh = figure;
 fc = 2;
end

function teardown(fh,fc)
 delete(fh);

function testColormapColumns(fh,fc)
 assertEqual(size(get(fh, 'Colormap'), fc), 3);

function testPointer(fh,fc)
 assertEqual(get(fh, 'Pointer'), 'arrow');

When I runtests it says:

Input argument "fc" is undefined.

Why is that? I done something wrong or it is unsupported in the current version of Matlab xUnit? How to circumvent that?

PS: Actually my MATLAB requires each function to have an end. I didn't wrote them here to keep consistency with the manual examples.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

过期以后 2024-08-12 07:53:24

该框架仅使用单个输出参数调用您的设置函数。如果您想从设置函数中传递更多信息,请将所有内容捆绑到一个结构中。

另外,这里是用 end 终止函数的规则。 (这些规则于 2004 年在 MATLAB 7.0 中引入,此后一直没有更改。)

如果文件中的任何函数以 end 终止,则该文件中的所有函数都必须以 end 终止。

嵌套函数必须始终以 end 结束。因此,如果文件包含嵌套函数,则该文件中的所有函数都必须以 end 结束。

classdef 文件中的所有函数和方法都必须以 end 结束。

The framework only calls your setup function with a single output argument. If you want to pass more information out from your setup function, bundle everything into a struct.

Also, here are the rules for terminating a function with end. (These rules were introduced in MATLAB 7.0 in 2004 and have not changed since then.)

If any function in a file is terminated with an end, then all functions in that file must be terminated with an end.

Nested functions must always be terminated with an end. Therefore, if a file contains a nested function, then all functions in that file must be terminated with an end.

All functions and methods in classdef files must be terminated with an end.

弥枳 2024-08-12 07:53:24

只需使用结构:

function test_suite = testSetupExample
 initTestSuite;

function [fh] = setup
 fh.one = figure;
 fh.two = 2;
end

function teardown(fh)
 delete(fh.one);


function testColormapColumns(fh)
 assertEqual(size(get(fh.one, 'Colormap'), fc.two), 3);

等。

Just use a struct:

function test_suite = testSetupExample
 initTestSuite;

function [fh] = setup
 fh.one = figure;
 fh.two = 2;
end

function teardown(fh)
 delete(fh.one);


function testColormapColumns(fh)
 assertEqual(size(get(fh.one, 'Colormap'), fc.two), 3);

etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文