GithubHelp home page GithubHelp logo

hypothesisworks / hypothesis-java Goto Github PK

View Code? Open in Web Editor NEW
101.0 11.0 8.0 40 KB

Advanced property-based (QuickCheck-like) testing for Java. Feasibility prototype.

Home Page: http://hypothesis.works

License: Other

Java 100.00%

hypothesis-java's Introduction

Hypothesis for Java feasibility prototype

Hypothesis is a modern property based testing system designed for mainstream languages. The original version is for Python, where it works extremely well.

This is a very rough prototype of what Hypothesis could look like in Java. It's not even close to being production ready software. It implements the core algorithm and a very crude API. It has only been lightly tested. It exists to prove that the concept is feasible, and it achieves that.

Things that it currently supports:

  1. The core Hypothesis engine with example generation and shrinking.
  2. Working JUnit integration.
  3. A very small prototype data generator library.

Things that are currently unimplemented:

  1. The example database. The model used is the Hypothesis one where data is represented in an easy to serialize format, so implementing this is a Simple Matter Of Code, but it doesn't affect the feasibility so I haven't bothered yet.
  2. The example mutator and shrinker are significantly less sophisticated than the Python one. There's no technical reason for this to be case, they're just based on an earlier prototype of the concept that works well enough for a tech demo.
  3. Most of the data generation library. This is in many ways where the bulk of the work in porting an implementation lies.
  4. Decent example printing. Right now it just uses toString, which is not ideal. Additionally it's not obvious what the best way to hook printing into JUnit is.
  5. A license that you would want to use it under.

Example usage

The Hypothesis Java prototype is build on JUnit's Rules functionality.

You use a TestDataRule as a source of data. Within a test you can then call draw on it with a strategy argument to get an example drawn from that strategy. You can call draw as many times as you want for tests that need multiple values.

Here's an example of testing a sorting function using this:

import org.junit.Rule;
import org.junit.Test;

import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

import static com.drmaciver.hypothesis.generators.Generators.integers;
import static com.drmaciver.hypothesis.generators.Generators.lists;
import static org.junit.Assert.assertTrue;

public class TestSortingAList {
    @Rule
    public final TestDataRule data = new TestDataRule();

    @Test
    public void testIsSortedAfterSorting(){
        List<Integer> ls = data.draw(lists(integers()));
        ls.sort(Comparator.naturalOrder());
        assertSorted(ls);
    }

    // Utility assertion function. Doesn't use any Hypothesis functionality.
    private <T extends Comparable<T>> void assertSorted(List<T> elements){
        if(elements.isEmpty()) return;
        Iterator<T> it = elements.iterator();
        T previous = it.next();
        while(it.hasNext()){
            T current = it.next();
            assertTrue(previous.compareTo(current) <= 0);
            previous = current;
        }
    }

}

Licensing

This is currently under the AGPL. That is not intended as a long term licensing strategy but gives me more flexibility for future plans. The most likely future licensing path is that there will be a core version available under the MPLv2 or Apache, with a proprietary extension library containing most of the strategies and some useful extensions. I could be persuaded to make the whole thing open source under Apache/MPLv2 but the persuasion would have to be financial.

Future Plans

I'm not very interested in supporting another fully fledged property based testing implementation of the calibre of the Python version of Hypothesis for free. As such this library is likely to remain a prototype without some financial backing to change that, in the form of paid development and/or ongoing support contracts.

If you are interested in a fully fledged Hypothesis for Java, please contact me to discuss terms.

hypothesis-java's People

Contributors

drmaciver 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

hypothesis-java's Issues

Usage example

Could you please add a minimal usage example to the README?

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.