Personally sometimes forgot to run tests before push my code, then the tests will fail in the CI, So I have to fix them locally then push and wait for our CI to run and see if it’s succeeding or not. (this took some time and maybe blocking the CI pipeline with no result), so I decided to avoid the human error by run tests automatically before each commit using git hooks
What’s Git hooks?
“Like many other Version Control Systems, Git has a way to fire off custom scripts when certain important actions occur. There are two groups of these hooks: client-side and server-side. Client-side hooks are triggered by operations such as committing and merging, while server-side hooks run on network operations such as receiving pushed commits. You can use these hooks for all sorts of reasons.”
How to do that?
- Find your pre-commit file usually, it’s in .git>hooks>pre-commit
- Open this file using your favorite text editor.
- Add this snippet to the file and replace MyAwesomeApp with your name.
xcodebuild \ -workspace MyAwesomeApp.xcworkspace \ -scheme MyAwesomeApp \ -sdk iphonesimulator \ -destination 'platform=iOS Simulator,name=iPhone 11 Pro Max,OS=13.5' \ test | xcpretty --test --color
Give it a try:
- Just a normal commit to your project and it will start running the tests and you’ll get something like that.
This is a really tiny tip to enhance your workflow but this git hook is very powerful, you do a lot with it like linting, your custom scripts …etc
Please leave a comment with your favorite git hooks if you have one 😊