Jens Schauder is software developer since 1997. He loves software development for the constant challenges and constantly changing environment. A great chance to learn and teach. He is also blogger, author of various articles and speaker at conferences. Jens is a DZone MVB and is not an employee of DZone and has posted 86 posts at DZone. You can read more from them at their website. View Full User Profile

Ratchet Tests with ScalaTest

  • submit to reddit

Ever found more broken things in a project than you possible could fix in one go? I’m exactly in that situation. We have a test which checks for certain dependencies in our project. The problem: We discovered that the test was broken and didn’t report all disallowed dependencies. We do know how to fix it, but if we do it will fail. And since there are many violations it will take quite some time to fix all of them. But of course we want to prevent even more violations of our dependency rules.

The solution could be a ratchet on the tests. A contraption which accepts broken tests, but which doesn’t allow tests to fail once they succeeded.

For ScalaTest you can create appropriate tests using this simple Ratchet trait

    package de.schauderhaft.ratchet
    import org.scalatest.AbstractSuite
    import org.scalatest.Suite
    import org.scalatest.TestFailedException
    trait Ratchet extends AbstractSuite {
        self : Suite =>
        private var tests = Set[String]()
        def ratchet(ratchetedTests : Set[String]) {
            tests = ratchetedTests
        override abstract def withFixture(theTest : NoArgTest) {
            if (tests.contains( {
                var failedToFail = false
                try {
                    failedToFail = true
                } catch {
                    case ex : TestFailedException =>
                if (failedToFail)
                    fail("Remove '%s' from the ratchet it doesn't fail anymore".format(
            } else

It adds a ratchet method to your suite. You pass it a Set of test names. These are the tests that you expect to fail. If they do fail, the Ratchet will convert that failure to a success. If a test which you expect to fail succeeds, the Ratchet will make sure it does fail with a message saying you should remove it from the tests expected to fail. Tests not registered with the ratchet method behave just as normal tests do. This is how the contraption looks in action with an example test suite: (Note tests which start witch ‘expected:’ do fail

    class RatchetTestDemo extends FunSuite with ShouldMatchers with Ratchet {
            "a failing test with ratchet does not fail",
            "expected: a succeeding test with ratchet fails"))
        test("a failing test with ratchet does not fail") {
        test("expected: a failing test without ratchet fails") {
        test("a succeeding test without ratchet succeeds") {
        test("expected: a succeeding test with ratchet fails") {



Published at DZone with permission of Jens Schauder, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)