使用 libgdx 的类的单元测试

Unit-testing of libgdx-using classes(使用 libgdx 的类的单元测试)

本文介绍了使用 libgdx 的类的单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过 libgdx 编写游戏;我正在使用 junit 框架来简化我的代码的单元测试.现在有一部分代码(地图生成器,一个将我自己的地图格式转换为 TiledMap 的类......)我需要彻底测试,但它使用 libgdx 代码:从文件处理到资产加载.我不打算以这种方式测试实际的图形输出或游戏本身:但我想测试单个组件(计算、资产访问......)以避免明显的错误.

I'm writing a game over libgdx; I'm using the junit framework to simplify unit-testing my code. Now there's part of the code (a map generator, a class converting my own map format into TiledMap...) which I need to test thoroughly, but it uses libgdx code: from file handling to asset loading. I'm not planning to test the actual graphical output, or the game itself, in this way: but I want to test the single components (calculation, asset access...) to avoid blatant errors.

我尝试在setUpBeforeClass"中做类似的事情.方法:

I've tried to do something like this in the "setUpBeforeClass" method:

    LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
    cfg.useGL20 = true;
    cfg.width = 480;
    cfg.height = 320;
    cfg.resizable = true;
    LwjglApplication app = new LwjglApplication( new TestApplicationListener(), cfg);

并在 tearDownAfterClass() 中调用:

And calling within tearDownAfterClass():

    Gfx.app.exit()

但它确实创建了一个我不需要的窗口,而且当我只需要初始化文件处理时,它似乎有点过分了.有没有更好的方法来初始化 libGDX 组件而不创建整个应用程序对象?谢谢.

But it does create a window I do not need, and seems overkill when all I need is the file handling initialized. Is there a better way to initialize the libGDX components without creating an entire application object? Thanks.

回顾它(感谢评论中的 Sam),我意识到需要 GL 访问权限(加载资产需要它),但这种方法似乎不起作用:图形库似乎没有被初始化.GDX 文档没有帮助.有什么线索吗?

Going back over it (thanks to Sam in the comments), I realize GL access is needed (loading assets requires it), but this approach does not seem to work: the graphic library does not seem to be initialized. GDX documentation hasn't helped. Any clue?

推荐答案

这个问题还没有回答,我很惊讶没有人指出 无头后端,非常适合这种情况.将它与您最喜欢的模拟库结合起来,您就可以开始使用了.

This question hasn't been answered and I am surprised nobody has pointed out the headless backend, which is ideal for this situation. Combine this with your favorite mocking library and you should be good to go.

public class HeadlessLauncher {
    public static void main(final String[] args) {
        final HeadlessApplicationConfiguration config = new HeadlessApplicationConfiguration();
        config.renderInterval = Globals.TICK_RATE; // Likely want 1f/60 for 60 fps
        new HeadlessApplication(new MyApplication(), config);
    }
}

这篇关于使用 libgdx 的类的单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:使用 libgdx 的类的单元测试

基础教程推荐