如何使用ModelJUnit实现指定时长(如15分钟)的模型驱动测试?
我正在对某应用系统执行模型驱动测试,希望将测试执行时长指定为15分钟,请问该如何实现?目前我的测试代码如下:
final Tester tester = new GreedyTester(new AgentSystemModelTests()); tester.setRandom(new Random()); tester.buildGraph(); tester.addListener(new StopOnFailureListener()); tester.addListener("verbose"); tester.addCoverageMetric(new TransitionPairCoverage()); tester.addCoverageMetric(new StateCoverage()); tester.addCoverageMetric(new ActionCoverage()); tester.generate(500); tester.printCoverage();
Solution
Great question! To restrict your model-driven test run to exactly 15 minutes (instead of stopping after 500 test steps), you'll need to switch from a count-based termination condition to a time-based one. Here's how to update your code:
Modified Code
import java.util.concurrent.TimeUnit; // Don't forget this import // ... your existing code ... final Tester tester = new GreedyTester(new AgentSystemModelTests()); tester.setRandom(new Random()); tester.buildGraph(); tester.addListener(new StopOnFailureListener()); tester.addListener("verbose"); tester.addCoverageMetric(new TransitionPairCoverage()); tester.addCoverageMetric(new StateCoverage()); tester.addCoverageMetric(new ActionCoverage()); // Replace the fixed count with a time-based execution tester.generate(15, TimeUnit.MINUTES); tester.printCoverage();
Key Explanations
- The core change is replacing
tester.generate(500)withtester.generate(15, TimeUnit.MINUTES). This instructs theGreedyTesterto continue executing test steps until 15 minutes have passed, regardless of how many test cases are generated in that time. - Make sure to add the
java.util.concurrent.TimeUnitimport to use the time unit enum (it lets you clearly specify minutes, seconds, hours, etc.). - All your existing setup (listeners, coverage metrics, randomization) remains valid—this change only affects when the test execution stops.
Edge Case Note
If for some reason your testing framework doesn't support the generate(long, TimeUnit) overload, you could implement a custom loop that checks elapsed time on each iteration. But based on the GreedyTester class name and code structure, the time-based overload is likely supported out of the box.
内容的提问来源于stack exchange,提问作者Axel
相关产品推荐
相关产品推荐

