I do version control with Git, and unit testing with QUnit. Sometimes I find a bug in my software that was not present in a past version. It's easy for me to write a unit test specifically for that bug.
Given that unit test, can I easily go trough all my past commits and test the build with that unit test, so that I can pinpoint which commit caused the breakage?
Git has a command to do exactly what you want, git bisect
In your case you want to use it as
git bisect run /path/to/script
, which will automatically test commits and performs a check with each commit to find the bad commit.So, your script would compile your sources, run your unit test suite and then give the corresponding exit code. The example section from the bisect manpage covers this nicely (including broken builds, merging hotfix commits, etc.)
Use
git bisect
for this, please see this page.Since you're testing JavaScript, you will probably have to run the tests by hand and run
git bisect good
andgit bisect bad
as appropriate. However, if you can run your unit test from the command line, then you can usegit bisect run
to have Git execute the test repeatedly and track down the faulty commit automatically:That's pure magic the first time you see it! :-)
You described the job of
git bisect
. The Git Book has a good tutorial.There is also some confusion in the terms of your question: When a test is used to guard against re-introduction of previously fixed errors or for bisection of buggy commits, it is called a regression test, not a unit test. The latter test is purely testing if a given small unit of code works, and is under heavy time constraints (TDD people run unit tests several dozen times in a day). For a large project, you usually have much longer regression tests than unit tests, so you might want to get the categories clean.
Yes, this is called
git bisect
, and was first introduced in git.Principle:
c1
;c1
you know fails, call itc2
;git bisect start c2 c1
.You can even restrict the bisect to subpaths if you know where it fails, and if you have a script which can run the test non interactively, use
git bisect run
.