如何测试私有目录中的功能?

发布于 2024-10-08 07:33:53 字数 1148 浏览 2 评论 0原文

我正在使用 MATLAB xUnit 来测试一些代码。我希望能够直接调用 private 目录中包含的一些函数。

这是一个简单的可复制设置: 创建一个包含两个文件夹 codetest 的项目目录。在 code 中,创建一个名为 private 的子目录,以便目录树看起来像

project_root
  code
    private
  test

code 目录中放置一个函数

function y = main()
y = sub();
end

private dir 放置一个函数

function y = sub()
y = 123;
end

test 目录中放置一个函数

function testsub()
assertElementsAlmostEqual(sub(), 123);
end

现在导航到测试目录并调用 runtests。您应该看到一条错误,指出 sub 未定义。

matlab 路径上不允许有 private 目录,因此这不是一个选项。我可以在 code 目录中放置一个函数来检索所有私有函数的句柄,但这看起来很hacky。

访问测试私有函数的最佳方式是什么?

编辑:

导航到私人目录的想法有问题。理论上,我可以调用,例如,

cd(privateDirectory);
suite = TestSuite.fromName(testDirectory);
suite.run

不幸的是,一旦您调用 run,测试框架就会导航到包含测试的目录。

I'm using MATLAB xUnit to test some code. I'd like to be able to directly call some functions that are contained in a private directory.

Here's a simple replicable setup:
Create yourself a project directory with two folders code and test. In code, create a subdirectory named private so you directory tree looks like

project_root
  code
    private
  test

In the code directory place a function

function y = main()
y = sub();
end

In the private dir place a function

function y = sub()
y = 123;
end

In the test dir place a function

function testsub()
assertElementsAlmostEqual(sub(), 123);
end

Now navigate to the test dir and call runtests. You should see an error saying that sub is undefined.

private directories aren't allowed on the matlab path so that isn't an option. I could possibly put a function in the code dir that retrieves handles to all the private functions but this seems hacky.

What is the best way to get access to test the private functions?

EDIT:

The idea of navigating to the private directory has a problem. In theory I could call, e.g.,

cd(privateDirectory);
suite = TestSuite.fromName(testDirectory);
suite.run

Unfortunately the testing framework navigates to the directory containing the tests as soon as you call run.

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

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

发布评论

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

评论(3

秋千易 2024-10-15 07:33:53

一种解决方案是从testsub导航到私有目录,获取函数句柄 到您想要使用的私有函数 STR2FUNC,然后导航回来使用该函数句柄进行测试。您甚至可以将这些步骤放入单独的辅助函数中以进行单元测试,如下所示:

function privateFcn = get_private_fcn(privateDir,fcnName)
  oldDir = cd(privateDir);         %# Change to the private directory
  privateFcn = str2func(fcnName);  %# Get a function handle
  cd(oldDir);                      %# Change back to the original directory
end

您可以在 testsub 中使用此函数,如下

function testsub()
  privateDir = '...\project_root\code\private';    %# The path to the private
                                                   %#   directory
  privateFcn = get_private_fcn(privateDir,'sub');  %# Call get_private_fcn
  assertElementsAlmostEqual(privateFcn(), 123);    %# Apply your test
end

所示: 尽管 MATLAB 编辑器为我提供了 M-Lint 使用函数的警告 CD:“MCC 使用 CD 功能存在问题。”我认为您可以安全地忽略此警告,因为 1) 函数 get_private_fcn 更改当前目录,然后在获取函数句柄后立即将其更改回来,2) 您不是 处理已部署的应用程序

One solution is to navigate to the private directory from within testsub, get a function handle to the private function you want using STR2FUNC, then navigate back to do the testing using that function handle. You could even put these steps in a separate helper function for your unit testing, like so:

function privateFcn = get_private_fcn(privateDir,fcnName)
  oldDir = cd(privateDir);         %# Change to the private directory
  privateFcn = str2func(fcnName);  %# Get a function handle
  cd(oldDir);                      %# Change back to the original directory
end

And you would use this function in testsub as follows:

function testsub()
  privateDir = '...\project_root\code\private';    %# The path to the private
                                                   %#   directory
  privateFcn = get_private_fcn(privateDir,'sub');  %# Call get_private_fcn
  assertElementsAlmostEqual(privateFcn(), 123);    %# Apply your test
end

This works, although the MATLAB Editor gives me an M-Lint warning for using the function CD: "MCC use of the CD function is problematic." I think it's safe for you to ignore this warning since 1) the function get_private_fcn changes the current directory, then immediately changes it back after getting the function handle and 2) you're not dealing with a deployed application.

断舍离 2024-10-15 07:33:53

虽然您不能将私有目录放在搜索路径上,但您可以 cd 到私有目录本身。

一旦到达那里,MATLAB 将看到这些函数,并且可以使用它们。这就是我在私有函数上测试自己的方法。

While you cannot put a private directory on your search path, you CAN cd to the private directory itself.

Once there, MATLAB will see the functions, and it can use them. This is how I do that testing myself on private functions.

李不 2024-10-15 07:33:53

私有目录的要点是具有非常有限的可见性的函数,即仅对code中的函数。因此,没有官方的方法(至少据我所知)可以让它们可见。

在我看来,检索 code/private 中任何内容的句柄的函数似乎是最干净的方法。

另一种方法是将 testsub 放在 code 内。这样做的优点是测试函数始终与它们要测试的代码放在一起,但您可能会觉得这“污染”了您的代码目录。

The point of private directories is to have functions that have very limited visibility, i.e. only to the functions in code. Thus, there is no official way (at least that I know of) to make them visible.

The function that would retrieve the handles to whatever is in code/private seems to me the cleanest way to go about it.

The alternative would be to place testsub inside code. This has the advantage that the test functions are always together with the code they're supposed to test, but you may feel this "pollutes" your code directory.

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