DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Ratchet Tests with ScalaTest

Ratchet Tests with ScalaTest

Jens Schauder user avatar by
Jens Schauder
·
Sep. 26, 11 · Java Zone · Interview
Like (0)
Save
Tweet
3.93K Views

Join the DZone community and get the full member experience.

Join For Free

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(theTest.name)) {
                var failedToFail = false
                try {
                    super.withFixture(theTest)
                    failedToFail = true
                } catch {
                    case ex : TestFailedException =>
                }
                if (failedToFail)
                    fail("Remove '%s' from the ratchet it doesn't fail anymore".format(theTest.name))
            } else
                super.withFixture(theTest)
        }
    }


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 {
        ratchet(Set(
            "a failing test with ratchet does not fail",
            "expected: a succeeding test with ratchet fails"))
     
        test("a failing test with ratchet does not fail") {
            fail
        }
     
        test("expected: a failing test without ratchet fails") {
            fail
        }
     
        test("a succeeding test without ratchet succeeds") {
        }
     
        test("expected: a succeeding test with ratchet fails") {
            println("hallo")
        }
    }

 

From http://blog.schauderhaft.de/2011/09/18/ratchet-tests-with-scalatest/

Testing

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Java Outsourcing, a Strong Business, and Management Approaches
  • Selenium vs. Protractor: What's the Difference?
  • Troubleshooting Memory Leaks With Heap Profilers
  • Federated Schema Design

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo