有没有办法为libGDX应用程序创建集成测试?

时间:2017-02-15 14:32:35

标签: unit-testing libgdx integration-testing

如果问题重复,我道歉,但我无法找到有关此问题的任何信息。

我知道我可以使用JUnit创建简单的单元测试,但我无法在android / iOS设备上运行它。如果我理解正确,我可以使用仪表单元测试,但它们仅适用于Android平台。在这种情况下,我无法从libGDX核心测试函数(我错了吗?)。所以,我很感兴趣,我如何在设备上运行我的测试?

1 个答案:

答案 0 :(得分:0)

测试 libGDX 应用程序不是一个简单的话题,但有了一个好的架构,它是可能的。关键点是将渲染部分与您要测试的业务逻辑分开。渲染总是需要一个 OpenGL 上下文,如果你试图在没有它的情况下运行它,就会中断。如果您不打算在无头构建服务器上运行它们,而只是在您的桌面上运行它们,则您实际上可以编写需要 OpenGL 的测试。

话虽如此,libGDX 应用程序的测试主要集中在 HeadlessApplication 的使用上,它使依赖于 libGDX 的代码可在您的测试环境中运行。如果你想在测试中开始整个游戏,你需要一个无头版本(这里是“MyGameHeadlessApplication”)。然后你可以像这样初始化它:

    private MyGameHeadlessApplication application;

    @Before
    public void setUp() throws Exception {
        HeadlessApplicationConfiguration config = new HeadlessApplicationConfiguration();
        config.renderInterval = 1F / 30F;
        application = new MyGameHeadlessApplication();
        new HeadlessApplication(this.application , config);
    }

为了测试依赖于 libGDX 的较小部分,有一个非常方便的库可用:gdx-testing project 包含一个 GdxTestRunner,它将您的测试包装在 HeadlessApplication 中并允许您进行类似的操作(来自 gdx - 测试示例):

@RunWith(GdxTestRunner.class)
public class MySuperTestClass {
    @Test
    public void bestTestInHistory() {
        // libgdx dependent code runs here
    }
}

最重要的是,我的资产文件夹有一个小问题,首先在测试中找不到。我通过在我的 workingDir 中为测试设置 build.gradle 解决了这个问题。当然,请确保拥有所有需要的依赖项(如果您在测试中需要,还可以使用 box2d)。在我的设置中,我在“核心”项目中进行了所有测试:

project(":core") {
    apply plugin: "java"

    test {
        project.ext.assetsDir = new File("./assets")
        workingDir = project.ext.assetsDir
    }

    dependencies {
        testCompile "com.badlogicgames.gdx:gdx-backend-headless:$gdxVersion"
        testCompile "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"
        testCompile "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-desktop"
        // ... more dependencies here ...

另见Unit-testing of libgdx-using classes