I'm currently following the ever-popular git workflow described here.
One thing it doesn't discuss is how to handle unit testing. Let's say I write some code and commit to develop
. I want to write a test for it, but I don't want it in my develop
history, I'd like to keep the tests separate so that when I look at git log
I only see changes to the main code. However, if I commit the test to a separate test
branch, I'll have to merge develop/test back and forth constantly, resulting in a ridiculous amount of merge commit clutter.
How do you manage unit testing in a git repository?
Unit testing often finds its place just after the build stage of the delivery pipeline. Developers commit their code and push it to a code repository.
The two primary branches in Git flow are main and develop. There are three types of supporting branches with different intended purposes: feature, release, and hotfix.
You definitely should put your tests into the repository. Tests are in my opinion part of the code and can help others immensely to understand it (if well written). Besides, they can help others when changing or contributing to your codebase.
Create a pre-commit git hook to execute your tests. In an example, Python projects use pytest to execute tests. Now each time you issue commit command to git it will run the script. The commit will fail when pytest fails. Effectively you will not be able to commit to git code which is not tested.
Multiple people create separate branches to work on their code and merge their changes into the main branch. Branches are meant to be temporary and should be deleted when work is completed. Branch names can be anything you’d like.
You can create a branch from a previous commit on an existing branch. Remember, a commit is just a snapshot in time of the files in a repository. You create a branch from a commit if you want to work on a specific snapshot of the files. Before creating the branch, you need the SHA-1 identifier of the commit.
You will be able to commit code locally, but when you run git push, before git executes the command, it will execute the pre-push script. If the script finishes with error, the push command will be aborted. You can run the git commit or git push command. The hook will be executed before the actual command runs.
Unit tests are part of your source tree: They evolve as your code evolves, and feature branches have their own unit tests that evolve as the features evolve. There's no reason to keep them in a separate branch.
Note that git log
accepts a path parameter, so if you want to avoid seeing test-only log entries, you can do the filtering at that point. I typically keep my src
and testsrc
source folders separate and adjacent, so I can see changes to one or the other easily just by specifying git log src
or git log testsrc
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With