Crafting Challenges
It's honestly simpler than it looks!

# C#
On the way!
# C++
On the way!
# Java
JUnit 4.x relies on annotations to flag methods and to shape how the test run behaves. The table below summarizes the annotations you'll reach for most often.
| Annotation | Description |
|---|---|
| @Test public void method() | The `@Test` annotation marks a method so it runs as a test method. |
| @Test (expected = Exception.class) | Fails when the method doesn't raise the expected exception. |
| @Test(timeout=100) | Fails when the method runs longer than 100 milliseconds. |
| @Before public void method() | Runs before every test. Handy for preparing the test environment (for example, loading input data or initializing the class). |
| @After public void method() | Runs after every test. Handy for tearing down the test environment (for example, deleting temporary data or restoring defaults). It can also free up memory by clearing out costly memory structures. |
| @BeforeClass public static void method() | Runs a single time, before any of the tests begin. Great for time-consuming setup work, such as opening a database connection. Methods carrying this annotation must be declared as static so that JUnit can use them. |
| @AfterClass public static void method() | Runs a single time once every test in the class has wrapped up, which makes it the natural home for tear-down chores such as closing an open database connection. Any method carrying this annotation must be declared as static so that JUnit will pick it up. |
| @Ignore | Skips over the annotated test method entirely. This comes in handy when the code beneath it has shifted but the matching test has not caught up yet, or when a single test simply takes too long to run on every pass. |
Assertion Methods
JUnit ships a bundle of static methods inside the Assert class that let you verify whether a given condition holds. By convention these helpers begin with assert and accept an error message alongside the expected and the actual result. Under the hood each one lines the actual value your test returned up against the value you expected, raising an AssertionException the moment that comparison fails.
The table just below rounds up each of these methods at a glance; anything wrapped in [] brackets is
| Statement | Description |
|---|---|
| assertEquals([String message], expected, actual, [tolerance]) | Confirms that two supplied values line up as equal. The optional `tolerance` sets how many decimal places have to match whenever you are weighing up `double`s or `float`s. Heads up: with arrays only the reference gets compared, never the contents, so reach for `assertArrayEquals`. |
| assertArrayEquals([String message], expecteds, actuals) | Confirms that a pair of array types hold matching contents. The array types you can hand it are: `byte[]`, `char[]`, `int[]`, `long[]`, `java.lang.Object[]`, and |
| assertNull([String message], object) | Verifies the given object is null. |
| assertNotNull([String message], object) | Verifies the given object is not null. |
| assertThat([String message], T actual, Matcher matcher) | Confirms that `actual` meets the condition laid out by the given `matcher`. |
| assertSame([String message], expected, actual) | Verifies that both variables point at the very same object. |
| assertNotSame([String message], expected, actual) | Verifies that the two variables point at distinct objects. |
| fail(String) | Forces the method to fail outright. Reach for it to prove a given branch of code never gets reached, or to stand in as a deliberately failing test before you have written the real logic. Its `String` argument is |
CoreMatchers DSL
The assertThat method opens the door to the CoreMatchers Domain Specific Language (DSL) when you are spelling out your tests. For the complete write-up, see it here.
| Static Method | Description |
|---|---|
| is(thing) | An overloaded, do-it-all method. It can test class membership, wrap itself around another matcher, or simply check two things |
| equalTo(T expected) | Passes when the actual value equals the one you expected. Feel free to lean on `is(T expected)` as a shorter |
| describedAs(String description, Matcher matcher) | Replaces the description shown whenever the `matcher` |
| allOf(Matcher... matchers) | Comes out true only when every single one of the matchers you pass in also |
| anyOf(Matcher... matchers) | Comes out true when at least one of the supplied `matchers` evaluates to true. |
| anything() | This matcher will always evaluate to true |
| not(T matcher) | Passes only when the wrapped `matcher` does not. |
# JavaScript
Test.assertEquals(actual, expected, message)
Confirms that the actual value is strictly equal (===) to the expected value. A clear, friendly message is shown whether the check passes or fails. The message argument is optional. When you supply one, it appears alongside the standard default message.
Test.assertNotEquals(actual, unexpected, message)
Confirms that the actual value is strictly not equal (!==) to the unexpected value. A clear, friendly message is shown whether the check passes or fails. The message argument is optional. When you supply one, it appears alongside the usual typical default message.
Test.assertSimilar(actual, expected, message)
Confirms that the actual value is strictly equal (===) to the expected value. Test.inspect is applied to wrap the values under comparison, so that values that merely look alike are treated as identical. A clear, friendly message is shown whether the check passes or fails. The message argument is optional. When you supply one, it appears alongside the default message.
Test.assertNotSimilar(actual, unexpected, message)
Confirms that the actual value is strictly not equal (!==) to the unexpected value. Test.inspect is applied to wrap the values under comparison, so that values that merely look alike are treated as identical. A clear, friendly message is shown whether the check passes or fails. The message argument is optional. When you supply one, it appears alongside the default message.
Test.expectError(message, fn)
Handy when you want to confirm that an error was supposed to be thrown. The message argument is optional, though writing one is a smart habit for the trickier test cases.
Test.expectNoError(message, fn)
Handy when you want to confirm that no error was supposed to be thrown. The message argument is optional, though writing one is a smart habit for the trickier test cases
Test.callCount(methodName) → Integer
Gives back the count of how many times a Test method was invoked. Handy when you build challenges that must check how the Test methods get used. Also handy when Test helper methods are off-limits inside a challenge solution. You can just check whether any forbidden calls were made.
Test.inspect(object) → String
Gives back a readable string form of the object. For plain objects toString() does the job, while for richer objects a JSON representation is produced instead.
Test.randomize(array) → Array
Test.randomNumber() → Integer
Test.randomToken() → String
Test.sample(array) → object
# PHP
On the way.
# Python
Test.assert_equals(actual, expected, message)
Confirms that the actual value matches the expected value. A clear, friendly message is shown whether the check passes or fails. The message argument is optional. When you supply one, it appears alongside the standard default message.
Test.assert_not_equals(actual, unexpected, message)
Confirms that the actual value does not match the unexpected value. A clear, friendly message is shown whether the check passes or fails. The message argument is optional. When you supply one, it appears alongside the usual typical default message.
Test.expect_error(message, thunk)
Handy when you want to confirm that an error was supposed to be thrown. The message argument is optional, though writing one is a smart habit for the trickier test cases.
# Ruby
On the way.
# Swift
On the way.