libgdx使用类的单元测试 [英] Unit-testing of libgdx-using classes

查看:149
本文介绍了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使用类的单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆