测试未导出的函数
我想使用 rebar 和 common_test 测试一些未导出的函数。什么是干净有效的方法来做到这一点?
我的倾向是在编译/测试时设置一个宏,并以这种方式打开/关闭模块中的export_all,具体取决于它是生产还是测试构建。我希望它很简单 - 例如,我不想在从生产切换到测试之前编辑配置文件,反之亦然。但是,我没有找到使用 rebar 将参数传递给 erl 编译器的方法。我错过了吗?
我知道 eunit 可以轻松测试未导出的函数,但我已经让 common_test 的基础设施运行良好,我现在不想改变我的工作流程。
I'd like to test some unexported functions using rebar and common_test. What is a clean and efficient way to do this?
My inclination would be to set a macro at compile/test time, and switch on/off export_all in the modules that way, depending on whether it's a production or test build. I want it to be easy--e.g., I don't want to edit a config file before switching from production to test and vice versa. However, I don't see a way to pass arguments to the erl compiler using rebar. Did I miss it?
I know eunit can test unexported functions easily, but I've already got my infrastructure for common_test working well and I don't want to change my workflow right now.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您为测试运行构建模块时,您只需将
+export_all
添加到您的erlc
选项即可。您可以使用rebar.config
文件中的{erl_opts, [export_all]}.
来执行此操作。我认为您不能在运行时向 rebar 传递额外的 erlc 参数(例如,您不能执行 rebarcompile +export_all)。You could just add
+export_all
to yourerlc
options when you build the modules for your test run. You can do this with{erl_opts, [export_all]}.
in yourrebar.config
file. I don't think you can pass extraerlc
arguments to rebar at runtime (e.g. you can't dorebar compile +export_all
) though.编辑:刚刚意识到您使用了
common_test
。如果不适用请忽略此回复!我有一种稍微更先进的方法来测试未导出的函数,其优点是不导出它们,从而使生产代码尽可能完整(只有在某些函数未导出但未检测到时才可能出现错误)测试期间是否导出所有函数)。
这就是使用 Rebar 和 EUnit 的工作原理。
在源文件中,添加以下行:
在
test
文件夹中,添加一个名为yourmodule_tests.hrl
的文件(例如,与yourmodule_tests.erl< /code> 在正常情况下)并添加以下内容:
将以下配置添加到
rebar.config
(如果您已经有erl_opts
,只需将新元组添加到其中list):当您运行
rebar eunit
时,Rebar将定义TEST
环境变量,并且您的代码将被“测试编译”。也就是说,将您的测试包含到模块中,并且测试将能够访问内部函数。如果您使用除 Rebar 以外的任何其他内容,只需确保在编译测试时使用
erlc -DTEST ...
编译代码即可。EDIT: Just realised you used
common_test
. Please ignore this reply if it doesn't apply!I have a slightly more advanced way of testing un-exported functions that has the benefit of not exporting them, thus keeping the production code as intact as possible (there might be faults that only occur if some functions are not exported, but is not detected during testing if all functions are exported).
This is how it works using Rebar and EUnit.
In your source file, add these lines:
In your
test
folder, add a file calledyourmodule_tests.hrl
(in contrast to, for example,yourmodule_tests.erl
in the normal case) and add the following contents:Add the following configuration to
rebar.config
(if you already haveerl_opts
, just add the new tuple to that list):When you run
rebar eunit
Rebar will define theTEST
environment variable and your code will be "test compiled". That is, including your tests into your module and the tests will be able to access internal functions.If you use anything else than Rebar, just make sure you compile your code with
erlc -DTEST ...
when your compiling for tests.您可以添加 rebar 在编译 eunit 时使用的其他编译选项。正如 archaelus 所指出的,您需要
erlc
的+export_all
选项。将该行添加到您的
rebar.config
文件中。 rebar.config.sample 文件中提到了这一点。You can add additional compile options that are used by rebar when compiling for eunit. As noted by archaelus, you need the
+export_all
option toerlc
. Add the lineto your
rebar.config
file. This is in mentioned in the rebar.config.sample file.