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

Tests seems not to run in isolation #14

Open
bangarharshit opened this issue Oct 17, 2017 · 1 comment
Open

Tests seems not to run in isolation #14

bangarharshit opened this issue Oct 17, 2017 · 1 comment

Comments

@bangarharshit
Copy link
Owner

bangarharshit commented Oct 17, 2017

Copied from mscharhag/oleaster#16
The default JUnit runner isolate tests in a way that every test gets executed with a new test-case instance. This ensures a fresh fixture instance for each test, even if the developer uses an implicit setup shortcut.

It seems to me that Oleaster tests work always with the same fixture in such a case. This is because the test-case instance is reused for all test runs (see snippet below for a better understanding of the use-case).

This behavior might lead to problems which are difficult to understand. Although I am not a particular friend of this setup practice, I doubt that people will follow a do-not-use-this-kind-of-implicit-setup convention. Maybe it would be better to ensure this kind of isolation as JUnit does by default.

@RunWith( OleasterRunner.class )
public class OleasterTest {

  Object fixture = new Object();

  {  
    it( "first", () -> {
      System.out.println( "first: " + fixture.hashCode() );
    } );

    it( "second", () -> {
      System.out.println( "second: " + fixture.hashCode() );
    } );
  }
}
@bangarharshit
Copy link
Owner Author

bangarharshit commented Oct 17, 2017

mscharhag said:
Thanks for bringing this up. You are right, all Oleaster tests use the same test instance at the moment.

The problem here is that lambda expressions are bound to the scope of the surrounding object. In order to run specs in complete isolation

a new test instance is required for each spec
the code of all surrounding suites needs to be executed for the new test instance
So far I think this would be a good thing. However, I need to think a bit more about the consequences.
I will look into this.

Hi marioluan,

thanks for your feedback.

The current behavior is this:

public class MyTest {{
  // executed once, before the first test runs
  describe("suite", () -> {
    // executed once, before the first test runs
    beforeEach(() -> {
      // executed before each test
    });
    it("test", () -> {
      // executed once
    });
  });
}}

When creating a new instance of the test class for each test, all the surrounding describe() blocks need to be executed again.

So the new behavior would be:

public class MyTest {{
  // executed before each test
  describe("suite", () -> {
    // executed before each test
    beforeEach(() -> {
      // executed before each test
    });
    it("test", () -> {
      // executed once
    });
  });
}}

This means:

using beforeEach() should not be necessary in most cases. Instead the surrounding describe() block can be used. I think this is ok, but it is different to Jasmine's behaviour.
There is no way to run initialization code once (before the first test case). I think the best solution to this is to support JUnits @BeforeClass and @afterclass annotations in Oleaster tests.
It breaks compatibility with test written for older Oleaster versions. While I don't like this one, I think it is ok. Oleaster is still in an early version.
Feel free to share your opinions.

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

1 participant