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"

  • Passing data between build steps in Jenkins
  • Cygwin gitk issue
  • Git - How to find all “unpushed” commits for all projects in a directory?
  • TortiseSVN svn+ssh Error: Unable to connect to a repository at URL … Network connection closed unexpectedly
  • Can not activate a virtualenv in GIT bash mingw32 for Windows
  • git remove all deleted files from entire history
  • Understanding Meld 3 way merge with Git
  • “git push” failed: “You did not specify any refspecs to push”
  • Git submodule update: reference is not a tree… but commit IS there
  • Clone failed unable to access “https://…” failed connect to github:443 no error
  • Is it possible to keep .gitignore files out of SVN?
  • How to make Head point to master in git?
  • 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.