Eunit学习笔记

 

首先将eunit.hrl包含进文件

-include_lib("eunit/include/eunit.hrl").

文件中以_test()结尾的函数为实际的测试函数(eunit会依次执行这些函数),以_test_()结尾的函数为测试生成函数,用来生成实际的测试函数,通常测试生成函数会返回一个描述测试函数的列表。

 

禁用eunit:

Erlc -DNOTEST my_module.erl

-define(NOTEST,true).

要在-include_lib("eunit/include/eunit.hrl"). 之前

启用eunit:

Erlc -DTEST my_module.erl

 

宏:

_test(Expr) Expr生成测试函数,相当于{?LINE,fun() -> (Expr) end}

 

assert(BoolExpr)

assertNot(BoolExpr)

assertMatch(GuardPattern,Expr)

assertEqual(Expect,Expr)

assertException(ClassPattern,TermPattern,Expr)

assertError(TermPattern,Expr)

assertExit(TermPattern,Expr)

assertThrow(TermPattern,Expr)

这些宏在失败时通常会抛出一个异常。在这些宏的前面加_所生成的宏(_assert())用来生成测试函数。

The assertError, assertExit, and assertThrow macros, are equivalent to using assertException with a ClassPattern of error,exit, or throw, respectively.

 

debugHere

debugMsg(Text)

debugFmt(FmtString,Args)

debugVal(Expr)

debugTime(Text,Expr)

 

这些宏会直接在console输出信息

 

EUnit captures standard output

If your test code writes to the standard output, you may be surprised to see that the text does not appear on the console when the tests are running. This is because EUnit captures all standard output from test functions (this also includes setup and cleanup functions, but not generator functions), so that it can be included in the test report if errors occur. To bypass EUnit and print text directly to the console while testing, you can write to the user output stream, as in io:format(user, "~w", [Term]). The recommended way of doing this is to use the EUnit Debugging macros, which make it much simpler.

 

Fixtures:Setup and Cleanup

{setup,Setup,Tests}

{setup,Setup,Cleanup,Tests}

setup函数的返回值会传到cleanup函数中。

上述代码段在执行test case时先执行setup函数,测试执行完后再执行cleanup函数,

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值