skinny85 / specnaz

Library for writing beautiful, RSpec/Jasmine/Mocha/Jest-style specifications in Java, Kotlin and Groovy
Other
34 stars 8 forks source link

Top-level description missing from terminal output when running tests #3

Closed skinny85 closed 7 years ago

skinny85 commented 7 years ago

When running Specnaz tests from the console, the top-level description (the one given to the describes method from the Specnaz interface) is not always shown when reporting the name of the tests.

This depends somewhat on the build tool being used, but, for Gradle (if you add:

test {
    testLogging {
        events "passed", "skipped", "failed"
    }
}

to your build.gradle), for the following, slightly modified StackSpec from the Readme.md:

import org.junit.Assert;
import org.specnaz.junit.SpecnazJUnit;

import java.util.Stack;

public class StackSpec extends SpecnazJUnit {{
    describes("A Stack", it -> {
        Stack<Integer> stack = new Stack<>();

        it.should("be empty when first created", () -> {
            Assert.assertTrue(stack.isEmpty());
        });

        it.endsEach(() -> {
            stack.clear();
        });

        it.describes("with 10 and 20 pushed on it", () -> {
            it.beginsEach(() -> {
                stack.push(10);
                stack.push(20);
            });

            it.should("have size equal to 2", () -> {
                Assert.assertEquals(2, stack.size());
            });

            it.should("have 20 as the top element", () -> {
                Assert.assertEquals(20, (int)stack.peek());
            });

            it.describes("and then the top element popped", () -> {
                it.beginsEach(() -> {
                    stack.pop();
                });

                it.should("have size 1", () -> {
                    Assert.assertEquals(1, stack.size());
                });
            });
        });
    });
}}

...the Gradle output is the following:

StackSpec > A Stack.should be empty when first created PASSED
StackSpec > with 10 and 20 pushed on it.should have size equal to 2 PASSED
StackSpec > with 10 and 20 pushed on it.should have 20 as the top element PASSED
StackSpec > and then the top element popped.should have size 1 PASSED

A correct output would probably look more like:

StackSpec.A Stack > should be empty when first created PASSED
StackSpec.A Stack > with 10 and 20 pushed on it.should have size equal to 2 PASSED
StackSpec.A Stack > with 10 and 20 pushed on it.should have 20 as the top element PASSED
StackSpec.A Stack > and then the top element popped.should have size 1 PASSED
skinny85 commented 7 years ago

I think fixing this would require changing the way Specnaz builds the tree of JUnit's Description objects. Right now, we create a new Description below the class Description, from the top-level String passed to describes. All of the tests are children of that Description. If we want to change the console output, we should probably instead switch to a single Description at the root of the tree, most likely ClassName.<TopLevelDescription> (StackSpec.A Stack in the case of the above example).

skinny85 commented 7 years ago

I've worked on this issue today, and unfortunately there's nothing we can do to fix this :(

That class name in the Gradle output above is emitted by JUnit, and there is no way to get rid of it, or modify it. It will always be the simple name of the test class being ran.

The console output is not the limit of the problems, anyway. In the HTML report, for example, the test cases are wrong - the nested groups formed by calling it.describes are listed next to the top-level test descriptions from calling describes in the Specnaz interface. I have no idea how to fix that either.

Hopefully, when JUnit 5 comes out (and with it the JUnit Platform), the integration with JUnit will become easier and less hacky. Until that time, I'm afraid there's nothing we can do to fix these issues.