问题
Given that I in my workspace I have an android project MyAndroidProject
and my tests project MyAndroidProjectTests
directories how could I write a pre-commit git hook that will run the tests in the MyAndroidProjectTests
and refuse to commit any code changes if the tests fail?
When I run tests on the terminal they usually have output like this:
com.mydomain.tests.Models.MyProjectTests:.......
Test results for InstrumentationTestRunner=.......
Time: 0.05
OK (10 tests)
What I'm unsure about is how to what to use to try to determine if the tests passed or failed other than parsing the output of the last line (e.g. OK
or FAILED
and I'm not sure I like that method so much. I ideally I'd like a status to be returned from the command I use to run tests on the terminal:
adb shell am instrument -w com.mydomain.tests/android.test.InstrumentationTestRunner
I'd like to use the response in a shell script that I could place in the .git/hooks/
folder as a pre-commit hook.
I would appreciate any info or links to other resources and much thanks in advance.
回答1:
git hooks are executed from your project/repo root - so you should be able to just use your command directly in a precommit hook:
#!/bin/bash
adb shell am instrument -w com.mydomain.tests/android.test.InstrumentationTestRunner
if adb
returns a none-zero exist code on failure - the commit will be aborted.
If you need to get the exit code for another purpose there are other questions indicating exactly how to do that.
来源:https://stackoverflow.com/questions/10234192/how-to-write-a-git-pre-commit-hook-that-prevents-committing-of-an-android-projec