Freecam icon indicating copy to clipboard operation
Freecam copied to clipboard

Initial unit tests

Open MattSturgeon opened this issue 1 year ago • 4 comments

Beginning work on unit tests, opening a draft PR for early feedback.

The key breakthrough was calling Bootstrap.bootStrap() before running tests; without bootstrapping minecraft, registries (etc) are not initialized and essentially no MC classes can be constructed. This apparently affects 1.17 & newer.
My solution is based on this GPL3 code, see comments below.

The goal of this PR is not to provide full test coverage, nor is this PR attempting to add integration tests with minecraft dependencies. That can all come in the future.

Instead this PR aims to enable basic unit testing functionality and provide one or two examples as a starting point.

Integration tests will likely require a test mod/run configured in loom, since the game will actually have to run to test integration. We might be able to do something with Mojang's GameTest system 🤔. That's all out of scope for this PR though.

JUnit 5 is chosen as the testing toolkit to run the tests. Upstream docs.

Mockito is chosen as a mocking framework to mock class dependencies. Upstream docs.

fabric-loader-junit tells JUnit to use fabric's Knot classloader. This enables things like mixin & Environment stripping.

See also

Writing good tests, in particular: don't mock a type you don't own; a long term goal should be to reduce hard dependencies on mc classes & also write integration tests.

AssertJ (alternative assertions syntax we could use instead).

Fixes #151

MattSturgeon avatar Jan 06 '24 22:01 MattSturgeon

Pinging @shartte, the original author of @BootstrapMinecraft & BootstrapMinecraftExtension, to ask whether we can include them under the MIT license.

MattSturgeon avatar Jan 06 '24 22:01 MattSturgeon

You can

shartte avatar Jan 06 '24 23:01 shartte

Putting this comment from the Architectury discord here for my own reference...

My repository might help you.

It has both JUnit tests and GameTest, but no Mixin support as I don't use Mixin. This file also enables you to run JUnit test with Forge and NeoForge. I use fabric-loader-junit for fabric. It depends on Mockito to inject hacky codes. - Kotori316

MattSturgeon avatar Feb 09 '24 13:02 MattSturgeon

Looks like https://github.com/neoforged/FancyModLoader/pull/54 will provide a Neoforge equivalent of fabric-loader-junit

MattSturgeon avatar Feb 15 '24 00:02 MattSturgeon