Create initial JUnit test with mocking - #2
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What is unit testing?
From GeeksForGeeks:
"Unit testing is the process of testing the smallest parts of your code, like it is a method in which we verify the code's correctness by running one by one. It's a key part of software development that improves code quality by testing each unit in isolation. You write unit tests for these code units and run them automatically every time you make changes. If a test fails, it helps you quickly find and fix the issue. Unit testing promotes modular code, ensures better test coverage, and saves time by allowing developers to focus more on coding than manual testing. A unit test is a small piece of code that checks if a specific function or method in is an application works correctly. It will work as the function inputs and verifying the outputs. These tests check that the code work as expected based on the logic the developer intended."
From WPILib:
"Unit testing is a method of testing code by dividing the code into the smallest “units” possible and testing each unit. In robot code, this can mean testing the code for each subsystem individually."
For more info, please take a look at the WPILib documentation for unit testing: https://docs.wpilib.org/en/stable/docs/software/wpilib-tools/robot-simulation/unit-testing.html
How to run tests
There are three options for running tests:
./gradlew buildcommand or by clicking "Build Robot Code" in the three-dot menu in the upper right corner of VSCode../gradlew testcommand or by clicking "Test Robot Code" in the three-dot menu in the upper right corner of VSCode.Complications
Testing hardware via unit tests is very tricky. When JUnit is running a test, the hardware doesn't actually exist. WPILib fixes this problem by simulating your components. However, you currently do not support simulated components in your code. This means that if you try to send a signal to your "motor," it won't behave like an actual motor and won't do anything. Because of this, it will be very difficult to unit test your components since they won't respond to any input...
How do we fix this? A couple ways:
ModuleKrakenobject and always have it return a certain state. This allows us to, instead of simulating the entire robot, only test the class we want to test and "fake" results of all of the objects it relies on (that you would find in its fields). This would require some refactor to your code (essentially rewrite your constructors to pass in the objects instead of creating the objects themselves).Since creating sims will require a much larger refactor, I've instead installed Mockito, a mocking library, and got started with unit testing using mocking. I'm hoping this gives you all a good starting point which you can build off of!
Unit testing is absolutely something the team should work towards. We should strive for complete code coverage under tests! I'm happy toget the process started so that it's less load on the team but everyone should learn this at some point. As always, feel free to ask any questions you may have about any of this.