Test.startTest() and Test.stopTest()

太长不读版: 

    简而言之,使用Test.startTest() and Test.stopTest()的目的有两条: 

    一条是规避系统的Governor Limits (start和stop之间的所有DML操作和主体代码是两个context)

    另一条是能够测试异步函数的执行(保证stop之后的代码在执行之前,start和stop包裹的异步代码已经执行完毕)

 

官方文档的解释: 

startTest()

Marks the point in your test code when your test actually begins. Use this method when you are testing governor limits.

Usage

You can also use this method with stopTest to ensure that all asynchronous calls that come after the startTest method are run before doing any assertions or testing. Each test method is allowed to call this method only once. All of the code before this method should be used to initialize variables, populate data structures, and so on, allowing you to set up everything you need to run your test. Any code that executes after the call to startTest and before stopTest is assigned a new set of governor limits.

stopTest()

Marks the point in your test code when your test ends. Use this method in conjunction with the startTest method.

Usage

Each test method is allowed to call this method only once. Any code that executes after the stopTest method is assigned the original limits that were in effect before startTest was called. All asynchronous calls made after the startTest method are collected by the system. When stopTest is executed, all asynchronous processes are run synchronously.

startTest 和stopTest成对出现,而且在一个testMethod里面只能出现一次。

startTest标志测试代码开始,stopTest标志测试代码结束。

所有的对于测试数据的设定,插入或者更新记录等等操作都应该在startTest之前完成,

然后把想要测试的code放在两者之间。

最后把测试的asserts放在stopTest之后执行。

 

startTest之前和stopTest之后的Governor Limits是同一个context,

startTest和stopTest之间的Governor Limits是另一个context。

而且,使用startTest和stopTest能够保证你所有的异步方法会在你的asserts代码之前执行完毕。

 

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值