Giter Site home page Giter Site logo

leifgehrmann / junit4-parameterized-tests-generator Goto Github PK

View Code? Open in Web Editor NEW
0.0 3.0 0.0 936 KB

Script to generate parameterized JUnit4 tests for Test Runners that do not support parameterized tests

License: The Unlicense

Java 46.50% JavaScript 53.50%
test-runners junit parameterized-tests java nodejs

junit4-parameterized-tests-generator's Introduction

junit4-parameterized-tests-generator

Build Status Code Coverage

This project is no longer maintained. Feel free to fork. If it is useful to anyone, let me know!

Script to generate parameterized JUnit-4 tests for test runners that do not support parameterized tests.

Purpose

JUnit-4 has a design flaw where test runners cannot be combined to perform multiple behaviors. For example, JUnit's Parameterized test runner cannot be combined with RobolectricTestRunner, or any other test runner because only one @RunWith annotation is permitted per class.

generateParameterizedTests.js solves this by parsing special comments in the test...

class StepUnitFormatterTests {
    //> Zero(0, "0 steps")
    //> Singular(1, "1 step")
    //> Plural(2, "2 steps")
    //> 999(999, "999 steps")
    //> Thousand(1000, "1,000 steps")
    //> Million(1000000, "1,000,000 steps")
    //> Decimal(3.3, "3 steps")
    //> Negative(-5, "0 steps")
    @ParameterizedTest
    public void testFormat(double numberOfSteps, String expected) {
        Assert.assertEquals(expected, StepUnitFormatter().format(numberOfSteps));
    }
}

...and converting them to individual tests, which get appended to the end of the file like this:

    // DO NOT EDIT BELOW THIS LINE 

    @Test
    public void testFormatZero() {
        testFormat(0, "0 steps")
    }

    @Test
    public void testFormatSingular() {
        testFormat(1, "1 step")
    }
    
    @Test
    public void testFormatPlural() {
        testFormat(2, "2 steps")
    }
    
    // Etc.
}

Installation

  1. Copy generateParameterizedTests.js anywhere into your Java project.
  2. Copy ParameterizedTest.java into your test source code folder.
  3. Update package x.x.x; in ParameterizedTest.java to reflect your project's package structure.

Running

Node must be installed, but npm install is not required. To generate the parameterized tests, run the following:

$ node generateParameterizedTests.js path/to/tests 

Running automatically using Gradle

To avoid having to run the generator manually, a task can be added to your build.gradle file to run the script on preBuild, or any other preferred Gradle task.

task generateParameterizedTests(type: Exec) {
    executable "sh"
    args "-c", "node scripts/generateParameterizedTests.js src/test"
}

preBuild.dependsOn generateParameterizedTests

Attribution

This code is dedicated to the public domain, so no attribution is needed.

This script is based on a similar implementation by Elliot Chance for generating parameterized tests in Swift.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.