Run testng test in Git pre-commit hook

How would I write a bash script that will run as a git pre-commit hook to fail if a specific testng test fails? I currently run my testng tests through maven surefire like this:

mvn clean test -Dtest="MyTestName"

  • Exclude .svn directories from grep
  • How do you set default application for `open` command in terminal based on file extension type?
  • Run git pull over all subdirectories
  • Add line break to git commit -m from command line
  • Modifying Jenkins Description for a build
  • git post-commit hook - script on committed files
  • Recommended continuous integeration system for Git?
  • How can you tell who merged which branch into git?
  • Make git pull (rebase) by default pull only from current downstream branch
  • VS2012 and VS2013 battle over .sln header
  • how to clear all files from the git from the “Changed but not updated:” list
  • Git sparse checkout with exclusion
  • One Solution collect form web for “Run testng test in Git pre-commit hook”

    If the pre-commit hook returns non-zero, then the commit is aborted before it even starts. In a bash script, by default the return value of the last command run is the return value of the script. By my understanding mvn clean test should return non-zero on failure, so your script should be as simple as:

    mvn clean test -Dtest="MyTestName"

    Then just name it pre-commit in your .git/hooks/ directory in your repo and run chmod ug+x on it to make sure it can be executed.

    Git Baby is a git and github fan, let's start git clone.