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"

  • git-upload-pack: command not found
  • git clone - how do I supress the “You appear to have cloned an empty repository” warning?
  • Bash shell is not taking the arguments in a way I would expect?
  • How to list all distinct extensions of tracked files in a git repository?
  • Git Initial Push Bad File Descriptor Error
  • state of git is confused by switching branches in a script
  • Checkout of git repository in pure PHP
  • How to get diff working like git-diff?
  • On diverged GIT branches
  • How to update a file in remote repo, without cloning that repo first?
  • github forking and upstream changes
  • Restore a local git directory with uncommitted changes
  • 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:

    #!/bin/sh
    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.