9

Jasmine is a nice unit testing framework for JavaScript. It not only tests your code, it provides a nice way to document it by:

  • Using a fluent BDD-ish way to define tests that in itself almost reads like a documentation
  • The test report reads like a documentation too

I'm wondering if anything comparable exists for C# and/or Java.

chiccodoro
  • 14,407
  • 19
  • 87
  • 130

7 Answers7

7

I just came across NJasmine on GitHub. I've never used it but thought this might help others like myself that want the awesome of Jasamine in C# unit tests.

From the GitHub:

NJasmine is a RSpec-ish test language inspired by the javascript test library Jasmine (https://github.com/fschwiet/DreamNJasmine) for C# / .Net programming.

given("some preconditions", () => {

    var range = 10;

    when("the system under test is ran", () => {

        var sut = new SystemUnderTest();

        bool score = arrange(() => sut.Fire(range));

        then("win!", () => {

            expect(() => score);
        });
    });
});

Available on Nuget: http://nuget.org/List/Packages/NJasmine

Again, I can't vouch for this as I haven't used it, but I hope this will help others make informed decisions.

HTH

Massimiliano Kraus
  • 3,638
  • 5
  • 27
  • 47
GraehamF
  • 1,971
  • 24
  • 24
5

JUnit 5 will be BDD-like, with @DisplayName, @Nested, and so on. You can have a look at the documentation.

The GA release is not here yet, but it should arrived soon (announced for late 2016).

JR Utily
  • 1,792
  • 1
  • 23
  • 38
  • It's great to see JUnit having build in support for it now. Making it possible to do BDD TDD in Java through the "default" testing framework – N.Schipper Oct 14 '17 at 19:24
4

Oleaster is a Java testing framework with clean simple syntax, extensively using Java 8 arrow functions. It is executed using JUnit runner.

Code sample from hompage:

@RunWith(OleasterRunner.class)
public class OleasterIntroductionTest {{
    describe("A suite", () -> {
        it("contains a spec with an expectation", () -> {
            expect(40 + 2).toEqual(42);
        });
    });
}}
czerny
  • 15,090
  • 14
  • 68
  • 96
  • What's the funny double-brace syntax? I admit I haven't seen it before. Is it new with Java 8, too? – chiccodoro Mar 12 '15 at 09:48
  • 2
    The block directly nested into class is called **instance initialization block** and is executed before constructor. See http://stackoverflow.com/questions/3987428/what-is-an-initialization-block or [official tutorial](http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html) for details. It was introduced before Java 8. – czerny Mar 12 '15 at 13:05
2

A very popular framework for testing Java (and Groovy) is Spock, whose tests also read like written specifications.

helpermethod
  • 59,493
  • 71
  • 188
  • 276
2

Have a look at Cucumber-JVM for implementing BDD. It is Java based but also works with JVM-based languages (Scala, Groovy).

If using a continuous integration system such as Jenkins, there is a reporting plugin called Cucumber Reports available.

Reimeus
  • 158,255
  • 15
  • 216
  • 276
1

An equivalent for Java would be JBehave

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
0

Ginkgo4j is a port of Ruby's RSpec BDD.

@RunWith(Ginkgo4jRunner.class)
public class MyTests {

  private SystemUnderTest systemUnderTest;

  private int range;

  private bool score;

  {
    Describe("SystemUnderTest", () -> {
      BeforeEach(() -> {
        systemUnderTest = new SystemUnderTest();
      });
      Context("#Fire", () -> {
        JustBeforeEach(() -> {
          score = systemUnderTest.Fire(range));
        });
        Context("given a range", () -> {
          BeforeEach(() -> {
            range = 10;
          });
          It("should return a winning score!", () => {
            assertThat(score, is(true));
          });
      });
    });
  }
}
Paul Warren
  • 2,411
  • 1
  • 15
  • 22