简体   繁体   中英

How to pause a Boost unit test?

I'm writing a unit test for a timer I created and I'm running into a problem I encounter often with other projects. I would like to create a unit test that starts my timer, waits a few moments, and then checks the value of my timer after the timer has been run. I could then test all the functionality I have in my timer.

Is there a way to do this? I don't see anything in the documentation. If this is not a good practice, please advise what I should do instead.

If you're unit-testing anything involving time you probably want to virtualize your notion of time so you can change it manually. Depending on what you're using for your timer this may be easier or harder to do (or built in), but once you've done it you can then write your unit tests using something like "setVirtualTime(time)".

Some advantages of this approach:

  1. The runtime of your unit tests don't depend on how long the timers in question are (once you have timers measured in hours / days this becomes important)
  2. Your unit tests are deterministic, and do not randomly fail depending on the vagaries of thread scheduling.

也许在单元测试中调用sleep()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM