Giter Site home page Giter Site logo

burst's Introduction

Burst

A unit testing library for varying test data.

DEPRECATED: Burst remains stable and functional, but you should check out TestParameterInjector from Google which is like Burst++. Unless there are major bugs in Burst which necessitate a patch release, no new development will be occurring.

Usage

Burst is a set of test runners which rely on enums for varying both the instantiation of test classes and the methods inside of them.

Define an enum for the property you wish to vary.

public enum Soda {
  PEPSI, COKE
}

The enum can be simple as above, or contain data and methods specific to what you are testing.

public enum Sets {
  HASH_SET() {
    @Override public <T> Set<T> create() {
      return new HashSet<T>();
    }
  },
  LINKED_HASH_SET() {
    @Override public <T> Set<T> create() {
      return new LinkedHashSet<T>();
    }
  },
  TREE_SET() {
    @Override public <T> Set<T> create() {
      return new TreeSet<T>();
    }
  };

  public abstract <T> Set<T> create();
}

Annotate your test class to use the BurstJUnit4 runner.

@RunWith(BurstJUnit4.class)
public class DrinkSodaTest {
  // TODO Tests...
}

An enum that appears on a test constructor will cause all the enclosed test to be run once for each value in the enum.

public DrinkSodaTest(Soda soda) {
  // TODO Do something with 'soda'...
}

Combine multiple enums for the combination of their variations.

public DrinkSodaTest(Soda soda, Sets sets) {
  // TODO Do something with 'soda' and 'sets'...
}

This will be called with the constructor arguments [PEPSI & HASH_SET, PEPSI & LINKED_HASH_SET, PEPSI & TREE_SET, COKE & HASH_SET, ..].

If your constructor is just setting fields, you can just annotate the fields with @Burst.

@RunWith(BurstJUnit4.class)
public class DrinkSodaTest {
  @Burst Soda soda;
  @Burst Sets sets;
  // TODO Tests...
}

This behaves just like the above example.

Note: Classes can either have constructors with arguments or annotated fields. A class with both will cause the test runner to throw an exception.

Methods may also be varied using one or more parameters that are enums.

@Test public void drinkFavoriteSodas(Soda soda) {
  // TODO Test drink method with 'soda'...
}

Having both constructor (or field) variation and method variation is supported.

@RunWith(BurstJUnit4.class)
public class DrinkSodaTest {
  private final Set<Soda> favorites;

  public DrinkSodaTest(Sets sets) {
    favorites = sets.create();
  }

  @Test public void trackFavorites() {
    // TODO ...
  }

  @Test public void drinkFavoriteSodas(Soda soda) {
    // TODO ...
  }
}

The trackFavorites test will be executed 3 times, once for each Sets value. The drinkFavoriteSodas test, however, is executed 6 times, for each of the three Sets values it runs twice for each Soda.

If a particular variation or variation combination does not make sense you can use assumptions to filter either directly in the test or as a custom rule.

Download

  • JUnit 4

    A test runner which can be used for JUnit 4.

    com.squareup.burst:burst-junit4:1.2.0
    
  • Core library

    Contains the core logic which creates the combinations of arguments for both constructors and method. Usually not useful on its own.

    com.squareup.burst:burst:1.2.0
    

Snapshots of the development version are available in Sonatype's snapshots repository.

License

Copyright 2014 Square, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

burst's People

Contributors

artem-zinnatullin avatar dependabot[bot] avatar dlubarov avatar dnkoutso avatar jakewharton avatar swankjesse avatar zach-klippenstein avatar zacsweers avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

burst's Issues

Kill JUnit 3 support.

Now that Android supports JUnit 4 via a "support" (cough) library shim we can nuke that crap from orbit.

Burst with Robolectric?

Is it possible to get burst working with Robolectric? I realise that adding two test runners would fail but have seen here that if one can be a rule, then both should be OK to run together.

I've looked for a TestRule for Robolectric but can't find one and looked for one in this project but same problem. Any idea how to get them both working or how to write a rule for one or the other?

Cheers!

BeforeClass being called for each Parameter

Currently, @BeforeClass methods get invoked once for the test class and then again every time a new Class Parameter combination gets tested the first time.
The code below will result in a sequence of messages like:

test: beforeClass
test: beforeClass
test: (param1 SMALL, param2 ENABLED) -> SquareBurst constructor
test: (size: SMALL, param2 ENABLED) -> before
test: (param1 SMALL, param2 ENABLED) -> test1
test: (param1 SMALL, param2 ENABLED) -> SquareBurst constructor
test: (size: SMALL, param2 ENABLED) -> before
test: (param1 SMALL, param2 ENABLED) -> test2
test: (param1 SMALL, param2 ENABLED) -> SquareBurst constructor
test: (size: SMALL, param2 ENABLED) -> before
test: (param1 SMALL, param2 ENABLED, methodParameter: SMALL) -> someShortTest
test: (param1 SMALL, param2 ENABLED) -> SquareBurst constructor
test: (size: SMALL, param2 ENABLED) -> before
test: (param1 SMALL, param2 ENABLED, methodParameter: MEDIUM) -> someShortTest
test: beforeClass
test: (param1 MEDIUM, param2 ENABLED) -> SquareBurst constructor
test: (size: MEDIUM, param2 ENABLED) -> before
test: (param1 MEDIUM, param2 ENABLED) -> test1
test: (param1 MEDIUM, param2 ENABLED) -> SquareBurst constructor
test: (size: MEDIUM, param2 ENABLED) -> before
test: (param1 MEDIUM, param2 ENABLED) -> test2
test: (param1 MEDIUM, param2 ENABLED) -> SquareBurst constructor
test: (size: MEDIUM, param2 ENABLED) -> before
test: (param1 MEDIUM, param2 ENABLED, methodParameter: SMALL) -> someShortTest
test: (param1 MEDIUM, param2 ENABLED) -> SquareBurst constructor
test: (size: MEDIUM, param2 ENABLED) -> before
test: (param1 MEDIUM, param2 ENABLED, methodParameter: MEDIUM) -> someShortTest
test: beforeClass
test: (param1 LARGE, param2 ENABLED) -> SquareBurst constructor
test: (size: LARGE, param2 ENABLED) -> before
test: (param1 LARGE, param2 ENABLED) -> test1
test: (param1 LARGE, param2 ENABLED) -> SquareBurst constructor
test: (size: LARGE, param2 ENABLED) -> before
test: (param1 LARGE, param2 ENABLED) -> test2
test: (param1 LARGE, param2 ENABLED) -> SquareBurst constructor
test: (size: LARGE, param2 ENABLED) -> before
test: (param1 LARGE, param2 ENABLED, methodParameter: SMALL) -> someShortTest
test: (param1 LARGE, param2 ENABLED) -> SquareBurst constructor
test: (size: LARGE, param2 ENABLED) -> before
test: (param1 LARGE, param2 ENABLED, methodParameter: MEDIUM) -> someShortTest
test: beforeClass
test: (param1 SMALL, param2 DISABLED) -> SquareBurst constructor
test: (size: SMALL, param2 DISABLED) -> before
test: (param1 SMALL, param2 DISABLED) -> test1
test: (param1 SMALL, param2 DISABLED) -> SquareBurst constructor
test: (size: SMALL, param2 DISABLED) -> before
test: (param1 SMALL, param2 DISABLED) -> test2
test: (param1 SMALL, param2 DISABLED) -> SquareBurst constructor
test: (size: SMALL, param2 DISABLED) -> before
test: (param1 SMALL, param2 DISABLED, methodParameter: SMALL) -> someShortTest
test: (param1 SMALL, param2 DISABLED) -> SquareBurst constructor
test: (size: SMALL, param2 DISABLED) -> before
test: (param1 SMALL, param2 DISABLED, methodParameter: MEDIUM) -> someShortTest
test: beforeClass
test: (param1 MEDIUM, param2 DISABLED) -> SquareBurst constructor
test: (size: MEDIUM, param2 DISABLED) -> before
test: (param1 MEDIUM, param2 DISABLED) -> test1
test: (param1 MEDIUM, param2 DISABLED) -> SquareBurst constructor
test: (size: MEDIUM, param2 DISABLED) -> before
test: (param1 MEDIUM, param2 DISABLED) -> test2
test: (param1 MEDIUM, param2 DISABLED) -> SquareBurst constructor
test: (size: MEDIUM, param2 DISABLED) -> before
test: (param1 MEDIUM, param2 DISABLED, methodParameter: SMALL) -> someShortTest
test: (param1 MEDIUM, param2 DISABLED) -> SquareBurst constructor
test: (size: MEDIUM, param2 DISABLED) -> before
test: (param1 MEDIUM, param2 DISABLED, methodParameter: MEDIUM) -> someShortTest
test: beforeClass
test: (param1 LARGE, param2 DISABLED) -> SquareBurst constructor
test: (size: LARGE, param2 DISABLED) -> before
test: (param1 LARGE, param2 DISABLED) -> test1
test: (param1 LARGE, param2 DISABLED) -> SquareBurst constructor
test: (size: LARGE, param2 DISABLED) -> before
test: (param1 LARGE, param2 DISABLED) -> test2
test: (param1 LARGE, param2 DISABLED) -> SquareBurst constructor
test: (size: LARGE, param2 DISABLED) -> before
test: (param1 LARGE, param2 DISABLED, methodParameter: SMALL) -> someShortTest
test: (param1 LARGE, param2 DISABLED) -> SquareBurst constructor
test: (size: LARGE, param2 DISABLED) -> before
test: (param1 LARGE, param2 DISABLED, methodParameter: MEDIUM) -> someShortTest

As can be seen, the @BeforeClass annotated method gets invoked multiple times.
My test methods need some resources that are read only and thus can be shared. However, it takes multiple seconds to create these, so I'd like to initialize them once for the entire test class.

I propose, BeforeClass should be called once for every test class, not for every new combination of Class Parameters. This is how JUnit's Parameterized Class does it as well.

Also, it would be great to have a new annotation that can be used to invoke a method every time a new Class Parameter combination is created, just like @BeforeClass does now, but give it access to the newly created Class Parameter combination, so that it can be used to do a one time setup and teardown for each Class Parameter combination.

Example Code:

@RunWith(BurstJUnit4.class)
public class SquareBurst {

    public enum Parameter1 { SMALL, MEDIUM, LARGE }
    public enum Parameter2 { ENABLED, DISABLED }
    public enum MethodParameter { SMALL, MEDIUM }

    @Before
    public void before() {
        System.out.println(String.format("test: (size: %s, param2 %s) -> before", size, parameter2));
    }

    @BeforeClass
    public static void beforeClass() {
        System.out.println("test: beforeClass");
    }

    private Parameter1 size;
    private Parameter2 parameter2;

    public SquareBurst(Parameter2 parameter2, Parameter1 size) {
        System.out.println(String.format("test: (param1 %s, param2 %s) -> SquareBurst constructor", size, parameter2));
        this.size = size;
        this.parameter2 = parameter2;
    }

    @Test
    public void someShortTest(MethodParameter methodParameter) {
        System.out.println(String.format("test: (param1 %s, param2 %s, methodParameter: %s) -> someShortTest",
                                         size, parameter2, methodParameter));
    }

    @Test
    public void test1() {
        System.out.println(String.format("test: (param1 %s, param2 %s) -> test1", size, parameter2));
    }

    @Test
    public void test2() {
        System.out.println(String.format("test: (param1 %s, param2 %s) -> test2", size, parameter2));
    }

}

JUnit 5 support

We should start looking at this sooner rather than later, although it may be too late to affect change if anything is lacking.

Support for field injection

We plan to support field injection, similar to @Parameter fields, as an alternative to constructor injection. It would look like

class SomeTestCase {
  @Burst TestCard card;

  // No explicit constructor.
}

This would make tests a bit shorter, and has a nice parity with @Inject, @Mock, @Captor, etc. which are common in unit tests.

Can't run individual test methods from IntelliJ

For tests with method variations it seems like you can't run an individual test from IntelliJ, only all the tests in a class.

java.lang.Exception: No tests found matching Method multiple(com.squareup.burst.MethodTest) from org.junit.internal.requests.ClassRequest@2e26c3a1
at org.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:35)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:41)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

Haven't really thought about if there's a fix or if it even matters.

Burst annotation doesn't work

I implemented libraries in Gradle file:
testImplementation('com.squareup.burst:burst-junit4:1.2.0')
testImplementation('com.squareup.burst:burst:1.2.0')
And receive next error when I run tests:

Task :app:kaptDebugAndroidTestKotlin FAILED
e: C:\Work\LT_projects\Sephora_android\app\src\androidTest\java\com\ltst\app\ui\screen\start\DeeplinkTest.java:16: error: cannot find symbol
@RunWith(BurstJUnit4.class)
^

Burst + AndroidTestCase support

Can't seem to run Burst with AndroidTestCase using a basic custom instrumentation runner (class with getAndroidTestRunner() which returns BurstAndroid).

Using burst-android 1.0.2

01-08 07:37:52.376 2546-2546/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: uk.co.imagitech.citb.operatives.plus, PID: 2546
java.lang.RuntimeException: Exception thrown in onCreate() of ComponentInfo{uk.co.imagitech.citb.operatives.plus.test/uk.co.imagitech.citb.operatives.plus.test.BurstTestRunner}: java.lang.RuntimeException: java.lang.IllegalStateException: junit.framework.TestSuite$1 requires at least 1 public constructor
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4338)
at android.app.ActivityThread.access$1500(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: java.lang.IllegalStateException: junit.framework.TestSuite$1 requires at least 1 public constructor
at com.squareup.burst.BurstAndroid.setTest(BurstAndroid.java:38)
at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:379)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4335)
            at android.app.ActivityThread.access$1500(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: junit.framework.TestSuite$1 requires at least 1 public constructor
at com.squareup.burst.BurstableConstructor.findSingle(BurstableConstructor.java:24)
at com.squareup.burst.BurstAndroid.explodeSuite(BurstAndroid.java:97)
at com.squareup.burst.BurstAndroid.explodeSuite(BurstAndroid.java:121)
at com.squareup.burst.BurstAndroid.setTest(BurstAndroid.java:36)
            at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:379)
            at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4335)
            at android.app.ActivityThread.access$1500(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)

API to limit set of values

Imagine you have

enum LocationSource {
  GPS,
  GLONASS,
  NETWORK,
  OTHER
}

And a test that should verify that behavior for GPS and GLONASS is the same:

@Test
@Only({GPS, GLONASS})
public void processHighAccuracyLocationSource(LocationSource locationSource) {
  // assert/verify something.
}

Same can be applied to constructor/field injection.

Needed this several times this week. Do you think such thing fits into the library?

Burst not working with Espresso

I setup project using Espresso 2.0 for testing. However, I couldn't get the TestCase's constructor injection to work. I use Gradle wrapper to execute test using ```./gradlew connectedAndroidTest'.

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'

    androidTestCompile 'com.squareup.spoon:spoon-client:1.1.2'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'
    androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
    androidTestCompile 'com.squareup.burst:burst-android:1.0.2'
}
public class BurstConstructorTest extends TestCase {
    private final EchoCollection echos;

    public BurstConstructorTest() {
        this(null);
    }

    public BurstConstructorTest(EchoCollection echos) {
        this.echos = echos;
    }

    public void testConstructorIsWorking() {
        assertNotNull(echos);
    }
}

I also tried changing to:

androidTestCompile ('com.squareup.burst:burst-junit4:1.0.2') {
        exclude module : 'hamcrest'
        exclude module : 'hamcrest-core'
        exclude module : 'junit'

    }
    androidTestCompile 'com.squareup.burst:burst-android:1.0.2'
    androidTestCompile 'com.squareup.burst:burst:1.0.2'
import com.squareup.burst.BurstJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import static junit.framework.Assert.assertNotNull;

@RunWith(BurstJUnit4.class)
public class BurstConstructorTest {
    private final EchoCollection echos;

    public BurstConstructorTest(EchoCollection echos) {
        this.echos = echos;
    }

    @Test
    public void testConstructorIsWorking() {
        assertNotNull(echos);
    }
}

This would results in "No Test found."

I modified test case to inject field but the test still fail.

public class BurstConstructorTest {
    @Burst EchoCollection echos;

    public BurstConstructorTest() {

    }

    @Test
    public void testConstructorIsWorking() {
        assertNotNull(echos);
    }
}

Did I miss something important to make it work?

Broken links in README

"If a particular variation or variation combination does not make sense you can use assumptions to filter either directly in the test or as a custom rule."

"assumptions" and "rule" links are broken in the README

Include the test class in the Description

I'm trying to design a JUnit4 Rule and have run into an issue with tests using Burst, because the Description passed to my rule returns null from getTestClass().

It looks like this line is the source of the Description in question, as it passes a custom string instead of a className.

Would Burst be open to fixing this? Something like:

return Description.createTestDescription(
    getTestClass().getJavaClass(),
    getName() + ":" + method.getName(),
    method.getAnnotations());

Is sufficient for my use case, though it does change the result of Description.getDisplayName(). If that works I'm happy to send a PR, otherwise are there other approaches you might be open to?

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.