You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用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) with tester.generate(15, TimeUnit.MINUTES). This instructs the GreedyTester to 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.TimeUnit import 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:46:55