使用Erlang EUnit还有更多人性化的测试消息吗? [英] More human-friendly tests messages with Erlang EUnit?

查看:99
本文介绍了使用Erlang EUnit还有更多人性化的测试消息吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我习惯于JavaScript测试,尤其是它的BDD框架和库,例如 Chai ,我可以在其中描述人类的测试.友好的方式和名称测试,例如字符串"UserProfile->更新名字",然后在运行测试时将这些消息作为输出.

I am used to JavaScript testing, especially its BDD frameworks and libraries like Chai where I can describe tests in a human-friendly manner and name tests with strings, e.g. "UserProfile -> Update First Name", then see these message as the output when running tests.

是否可能以更自然的方式编写Erlang测试,或者至少将描述集成到测试运行过程中,而不仅仅是将它们作为注释,还可以查看失败的测试的名称?

Is it possible to write Erlang tests in a more natural way, or at least, integrate descriptions in to tests run process, not just have them as comments, but see the name of a test which failed?

推荐答案

是.要向测试添加描述,测试必须是测试对象",而不是普通测试.例如,更改此测试:

Yes. To add descriptions to tests, the tests need to be "test objects" instead of plain tests. For example, change this test:

foo_test() ->
    run_foo(),
    ensure_foo_works().

对此:

foo_test_() ->
    ?_test(
      begin
        run_foo(),
        ensure_foo_works()
      end).

也就是说,函数的名称应以_test_结尾,并且测试的主体应包装在?_test宏中.还有其他这样的包装宏"以下划线开头.例如,一个简单的断言可以这样重写:

That is, the name of the function should end with _test_, and the body of the test should be wrapped in a ?_test macro. There are other such "wrapper macros" starting with an underscore; for example, a simple assertion can be rewritten like this:

%% before
check_foo_test() ->
    ?assertEqual(ok, foo()).

%% after
check_foo_test_() ->
    ?_assertEqual(ok, foo()).

一旦有了测试对象",就可以将其包装在一个元组中,其中第一个元素是字符串:

Once you have a "test object", you can wrap it in a tuple, where the first element is a string:

foo_test_() ->
    {"Ensure that foo works",
     ?_test(
       begin
         run_foo(),
         ensure_foo_works()
       end)}.

check_foo_test_() ->
    {"Check that foo is ok", ?_assertEqual(ok, foo())}.

测试失败时,将打印这些描述.如果您以详细模式运行eunit,则在执行测试时也会打印它们.

Those descriptions will be printed when the test fails. If you run eunit in verbose mode, they will be printed when the test executes as well.

这篇关于使用Erlang EUnit还有更多人性化的测试消息吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆