Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial unit tests #153

Draft
wants to merge 28 commits into
base: main
Choose a base branch
from
Draft

Conversation

MattSturgeon
Copy link
Member

@MattSturgeon MattSturgeon commented Jan 6, 2024

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
Copy link
Member Author

MattSturgeon commented Jan 6, 2024

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

@shartte
Copy link

shartte commented Jan 6, 2024

You can

@MattSturgeon MattSturgeon added enhancement New feature or request dev Only affects developers labels Jan 9, 2024
- '1.18'
- '1.19'
pull_request:
branches: *branches
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The workflow is not valid. .github/workflows/main.yml:
Anchors are not currently supported. Remove the anchor 'branches'

I guess GitHub doesn't like yaml anchors...

Suggested change
branches: *branches
branches:
- 'main'
- '1.16'
- '1.17'
- '1.18'
- '1.19'

@MattSturgeon
Copy link
Member Author

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
Copy link
Member Author

Looks like neoforged/FancyModLoader#54 will provide a Neoforge equivalent of fabric-loader-junit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dev Only affects developers enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unit testing
2 participants