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 TestFixture #1336

Closed
wants to merge 7 commits into from
Closed

Add TestFixture #1336

wants to merge 7 commits into from

Conversation

kcooney
Copy link
Member

@kcooney kcooney commented Jul 4, 2016

This is a prototype of test fixtures, an extension point designed to replaced rules. The core class is a new TestFixture interface that can be implemented in users' tests or put in a common location to be shared by multiple tests.

The TestFixture API was designed so that it could be supported by JUnit Vintage tests as well as JUnit Jupiter tests. In the first commit I show integration with Vintage tests. In the second commit, as a proof of concept, I updated TestCase to support TestFixture (I figured if JUnit Legacy tests could support them, anything could).

Although designed as a possible replacement for rules, test fixtures are in some ways less powerful, but in other ways more powerful. A fixture can cause code to be executed 1) before a test (or suite), 2) after a test (or suite) and/or 3) between the successful completion of the test (or suite) and any tear down calls (@After,etc) have been called. Unlike rules, however, they cannot be used to cause a failing test to pass, or to run a test multiple times.

Test fixtures are also easy to compose. Here is an example of a test fixture using another one:

public class TestWithWebDriver implements TestFixture {
  private final ServerStarter server = new ServerStarter(); // another fixture
  private final WebDriver webDriver;

  public void initialize(FixtureContext context) throws Exception {
    server.initialize(context); // start the server
    webDriver = new FirefoxDriver();
    driver.get("http://localhost+" + server.getPort()); // open the home page
  }
}

Here's an example how one might might run two fixtures in order:

private final TestFixture firstFixture = ...
private final TestFixture secondFixture = ...

@Fixture
public final TestFixture initializeInOrder = new TestFixture() {

  public void initialize(FixtureContext context) throws Exception {
    firstFixture.initialize(context);
    secondFixture.initialize(context);
  }
}

See the TemporaryDirectory class for an example fixture.

As it happens, test fixtures are also easier to test than rules are. Note that TemporaryDirectory does not have any methods that were added only for testing purposes, but the tests in TemporaryDirectoryTest have the same coverage.

Note that this change is missing many tests, and would likely require some framework. I just wanted to send this as a concrete proposal for a simple but powerful extension point that can be supported by multiple styles of JUnit tests.

@kcooney
Copy link
Member Author

kcooney commented Jul 16, 2016

@marcphilipp FYI I added a commit with improved Javadoc. Also, now the fixtures have access to the class and method annotations.

@kcooney
Copy link
Member Author

kcooney commented Sep 12, 2016

@marcphilipp any more thoughts on this proposal?

As I said in the JUnit5 issue, I could do this outside of JUnit but I would need a way to install global rules. In the past @dsaff wasn't thrilled with global rules (in part because would be nothing in the test indicating that the test has additional behavior applied to it). Maybe you feel differently?

@marcphilipp
Copy link
Member

I've now (finally) responded to your comment in junit-team/junit5#169 (comment). Sorry for the delay!

@kcooney
Copy link
Member Author

kcooney commented Sep 25, 2016

Created pull request to add the core classes to opentest4j: ota4j-team/opentest4j#26

@kcooney
Copy link
Member Author

kcooney commented Nov 30, 2016

Moved to opentest4j

@kcooney kcooney closed this Nov 30, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants