question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

It would be possible to unit test a GDX game if not for one thing.

See original GitHub issue

The headless backend helps with writing tests, up until you need access to something that uses OpenGL, like a SpriteBatch. Some people get around this by mocking Gdx.gl with something like Mockito.

I personally think that mocking an entire library like OpenGL is bad practice. So my solution is to just create a hidden window that has an active OpenGL context. Check out this gist for an example. With that I can test anything that uses OpenGL. No need to mock anything.

This would be possible out of the box if not for one simple thing: The backend starts the main loop inside the constructor. That’s the only reason I had to write the TestApplication class in that gist. I’m talking about this code right here.

If that was in a separate method, you could easily unit test a game just by instantiating the application with an invisible window, and disabling the audio.

ApplicationListener game = new Game();

Lwjgl3ApplicationConfiguration config = new Lwjgl3ApplicationConfiguration()
config.setInitialVisible(false);
config.disableAudio(true);

Lwjgl3Application testApplication = new Lwjgl3Application(game, config);

The only difference would be that you wouldn’t start the app and run the main loop. Then anything that makes calls to Gdx.gl would be available to your tests because you’d actually be testing against a real OpenGL context.

Then the desktop launcher would just need one extra step to start the game. For example, if the method that starts the main loop is called run:

new Lwjgl3Application(new Application(), config).run();

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:16 (10 by maintainers)

github_iconTop GitHub Comments

5reactions
NathanSweetcommented, Apr 18, 2020

I just roll my face on the keyboard and then let the users find any bugs. I find it helps a lot to avoid writing bugs in the first place.

3reactions
tommyettingercommented, Apr 15, 2020

I kinda doubt the libGDX devs will want to break compatibility for every single existing game for this. Unit tests, like the ones libGDX uses to test PRs, sometimes need to run on headless machines and don’t have this as a valid option anyway. Invisible and silent unit tests also have a limited range of behavior they can actually detect; games often need a human being to point out that, say, too many zangulorgs are spawning in an area, or in a visually-obvious pattern, and not enough blanyayools spawn in the same place, or their AI makes poor decisions. When unit tests are an excellent option, they are often checking non-subjective, non-perceptual things, like “Was an Exception thrown and uncaught during routine data structure manipulation?” or “Does the name entry validation function reject the empty string as a name?” Sometimes these need OpenGL to check, like when examining errors in shader compilation, but many more OpenGL issues need to be caught on corner-case hardware than can be caught on one machine with invisible graphics.

It sucks, but I don’t see unit testing catching on in game development (outside of situations where the logic can be tested independently) any time soon.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unit-testing of libgdx-using classes - Stack Overflow
Now there's part of the code (a map generator, a class converting my own map format into TiledMap...) which I need to test...
Read more >
Automated game mechanics testing of the entire application ...
I'm looking for a way to test my game. However, I am not just looking for unit tests, but would like to "run"...
Read more >
Beat the High-Score: Build a Game Using libGDX and Kotlin ...
Recording brought to you by American Express https://americanexpress.io/kotlin-jobsPlaying games is fun but making games is even better, ...
Read more >
Day 3: Understanding the libGDX Framework - Kilobolt
We could do this by copying the Game class and then providing the implementation to the missing method - it is actually just...
Read more >
Overlap2D survival guide for #libGDXJAM - libGDX
So you are planning to join #libGDXJAM and you are considering (or already ... same for buttons) If you do not have a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found