redesigning JUnit asserts

Discussion in 'Java' started by martinus, Aug 4, 2006.

  1. martinus

    martinus Guest

    Hello everybody,

    I am thinking of writing a replacement / addition for junit's assert
    system. I don't like how it is currently done because it is so
    inflexible. After playing around a bit, I came up with an API that
    looks like this:

    public void testInt() {
    int num = 4+1;
    ensure(num).is(5);
    ensure(num).either(4, 5, 6);
    ensure(num).between(3, 10);
    }

    public void testDouble() {
    double val = 4 + 0.4;
    // an epsilon is mandatory when comparing double
    ensure(val).is(4.4, 0.0001);
    ensure(val).between(4.3, 4.5);
    }

    public void testArray() {
    int[] a = { 1, 2, 3, 4, 5 };
    ensure(a).contains(3);
    ensure(a).size().either(5, 6);
    ensure(a).contains(2, 3, 4);
    ensure(a).contains().either(3, 10, 11);
    ensure(a).contains().neither(6, 7, 9);
    ensure(a).contains().any(3, 4);
    ensure(a).contains().all(3, 4, 5);
    ensure(a).isSorted();
    ensure(a).isUnique();
    }


    What do you think about such an API?

    Martin
    PS: Here is my blog about this
    http://martin.ankerl.org/2006/08/02/redesigning-junit-asserts/
    martinus, Aug 4, 2006
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ken
    Replies:
    5
    Views:
    1,438
    Guus Bosman
    May 22, 2004
  2. KKramsch

    newbie Q: C's asserts in C++

    KKramsch, Dec 3, 2004, in forum: C++
    Replies:
    10
    Views:
    631
    Alex Vinokur
    Dec 7, 2004
  3. Timothy Grant

    py.test munging strings in asserts?

    Timothy Grant, Apr 21, 2006, in forum: Python
    Replies:
    1
    Views:
    313
    Johnny deBris
    Apr 21, 2006
  4. Maxim Yegorushkin

    Redesigning a debug API

    Maxim Yegorushkin, Jul 13, 2005, in forum: C++
    Replies:
    3
    Views:
    275
    Thomas Lorenz
    Jul 13, 2005
  5. Replies:
    4
    Views:
    407
Loading...

Share This Page