简体   繁体   English

AnyLogic代理代码如何配置单元测试?

[英]How to configure unit testing for AnyLogic agent code?

How do you configure unit testing framework to help develop code that is part of AnyLogic agents?您如何配置单元测试框架来帮助开发属于 AnyLogic 代理的代码?

To have a suitable test driven development rhythm, I need to be able to run all tests in a few seconds.为了拥有合适的测试驱动开发节奏,我需要能够在几秒钟内运行所有测试。 I thought of exporting the project as a standalone application (jar) each time, but that's pretty slow.我想每次都将项目导出为独立应用程序 (jar),但这很慢。

I thought of trying to write all the code outside AnyLogic in separate classes, but there are many references to built-in AnyLogic classes, as well as various agents.我想尝试在单独的类中编写 AnyLogic 之外的所有代码,但是有很多对内置 AnyLogic 类以及各种代理的引用。 My code would need to refer to these somehow, and I'm not sure how to do that except by writing the code inside AnyLogic.我的代码需要以某种方式引用这些,我不知道该怎么做,除非在 AnyLogic 中编写代码。

I wonder if there's a way of adding the test runner as a dependency, and executing that test runner from within AnyLogic.我想知道是否有一种方法可以将测试运行器添加为依赖项,并从 AnyLogic 中执行该测试运行器。

Does anyone have a setup that works nicely?有没有人有一个运行良好的设置?

This definitely requires some advanced Java, but testing, especially unit testing is too often neglected in building good robust models.这肯定需要一些高级的 Java,但是测试,尤其是单元测试在构建良好的健壮模型时经常被忽视。 I hope this simple example is enough to get you (and lots of other modellers) going.我希望这个简单的例子足以让您(和许多其他建模者)继续学习。

For Junit testing, we make use of two libraries that you can add as a dependency to your model.对于 Junit 测试,我们使用两个库,您可以将它们添加为 model 的依赖项。

在此处输入图像描述

Now there are two main types of logic that you will want to test in simulation models.现在有两种主要类型的逻辑需要在仿真模型中进行测试。

  1. Functions in Java classes Java类中的函数
  2. Model execution Model 执行

Type 1: Suppose I have this very simple Java class类型 1:假设我有这个非常简单的 Java class

public class MyClass {

    public MyClass() {
    }
    
    public boolean getResult() {
        return true;
    }
}

And I want to test the function getResult()我想测试 function getResult()

I can simply create a new class and create a function that I annotate with the @Test modifier and then also make use of the assertEquals() method, which is standard in junit testing我可以简单地创建一个新的 class 并创建一个 function,我用 @Test 修饰符进行注释,然后还使用assertEquals()方法,这是 junit 测试中的标准方法

import org.junit.Test;
import static org.junit.Assert.assertEquals;
    
public class MyTestClass{

    @Test
    public void testMyClassFunction1() {
        boolean result = new MyClass().getResult();
        assertEquals("The value of the test class 1", result, true);
    }

Now comes the AnyLogic specific implementation (there are other ways to do this but this is the easiest/most useful, you will see in a minute)现在是 AnyLogic 的特定实现(还有其他方法可以做到这一点,但这是最简单/最有用的,您将在一分钟内看到)

You need to create a custom experiment您需要创建自定义实验

在此处输入图像描述

Now if you run this from the Run Model button you will get this output现在,如果你从运行 Model 按钮运行它,你将得到这个 output

在此处输入图像描述

SUCCESS

Run: 1
Failed: 0


You can obviously update and change the output as to your liking您显然可以根据自己的喜好更新和更改 output

Type 2: Suppose we have this very simple model类型 2:假设我们有这个非常简单的 model

在此处输入图像描述

And the function getResult() simply returns an int of 2.而 function getResult()只返回一个int 2。

Now we need to create another custom experiment to run this model现在我们需要创建另一个自定义实验来运行这个 model

在此处输入图像描述

And then we can write a test to run this Custom Experiment and check the result然后我们可以编写一个测试来运行这个自定义实验并检查结果

Simply add the following to your MyTestClass只需将以下内容添加到您的MyTestClass

@Test
    public void testMyClassFunction2() {
        int result = new SingleRun(null).runExperiment();
        assertEquals("Value of a single run", result, 2);
    }

And now if you run the RunAllTests customer experiment it will give you this output现在,如果您运行RunAllTests客户实验,它将为您提供此 output

SUCCESS

Run: 2
Failed: 0

This is just the beginning, you can read up tons on using junit to your advantage这只是开始,您可以阅读大量有关如何使用 junit 的文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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