Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to use datasource in class with @Nested Tests #1411

Closed
kiryl-naurotski opened this issue May 8, 2018 · 8 comments
Closed

Add ability to use datasource in class with @Nested Tests #1411

kiryl-naurotski opened this issue May 8, 2018 · 8 comments

Comments

@kiryl-naurotski
Copy link

kiryl-naurotski commented May 8, 2018

Overview

Right now tests located in a @Nested class run only after tests in enclosing classes have finished their execution. Such behavior doesn't allow one to run test classes with different values.

If we run the following test class:

public class StackTest {
    static int number;

    @ParameterizedTest
    @ValueSource(ints = {1, 2, 3, 4})
    @DisplayName("is instantiated with new Stack()")
    void isInstantiatedWithNew(int numbers) {
        number = numbers;
    }

    @Nested
    @DisplayName("when new")
    class WhenNew {

        @Test
        @DisplayName("is empty")
        void isEmpty() {
            System.out.println("Is empty: " + number);
            assertTrue(true);
        }

        @Nested
        @DisplayName("after pushing an element")
        class AfterPushing {

            @Test
            @DisplayName("it is no longer empty")
            void isNotEmpty() {
                System.out.println("is not empty: " + number);
                assertTrue(true);
            }
        }
    }
}

... we will see that all nested tests received only the last number (4).

Is there any way to send all provided numbers to nested tests?

Related Issues

@sbrannen
Copy link
Member

sbrannen commented May 8, 2018

Is there any way to send all provided numbers to nested tests?

There is no built-in mechanism for achieving that.

If you just want the list of numbers, you could of course store them in a field of type List<Integer> instead of only storing the last one in a field of type int.

@sbrannen
Copy link
Member

sbrannen commented May 8, 2018

The ability to run test classes with different values (i.e., to parameterize test classes) will be introduced in conjunction with #871/#878.

I am therefore closing this issue as a duplicate.

Feel free to provide further input in #871/#878.

Thanks!

@kiryl-naurotski
Copy link
Author

Numbers was just an example.
On the project I use root test to trigger flow and check if it triggered successfully.
Nested tests I use to check output from each component in the flow.

@sbrannen
Copy link
Member

sbrannen commented May 8, 2018

Do the proposals in #871 and #878 address your needs?

@kiryl-naurotski
Copy link
Author

Also for some reason class with nested tests runs faster, than class with single test with the same number of checks and actions

@sbrannen
Copy link
Member

sbrannen commented May 8, 2018

Also for some reason class with nested tests runs faster, than class with single test with the same number of checks and actions

I cannot imagine how that could be the case since the execution of a class along with its nested classes technically requires more work.

So, if you can provide a small project that proves that, we'd be interested in seeing that (in a separate GitHub issue).

Cheers

@kiryl-naurotski
Copy link
Author

kiryl-naurotski commented May 8, 2018

Do the proposals in #871 and #878 address your needs?

It seems that yes

@sbrannen
Copy link
Member

sbrannen commented May 8, 2018

It seems that yes

Great. Thanks for the feedback!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants