Popcorn - Data Driven Testing with JSON

October 26th, 2009 by asmyczek

Are you looking for a quick, easy and flexible way to create JSON objects to test your JSON service? Or maybe a tool to mock server responses to test some JavaScript browser code? If so, you might be interested in Popcorn.

Popcorn is a JavaScript embedded DSL design to generate any kind of JSON object. It comes with a basic set of generators for most common JavaScript types, and combinators to build new generators for any kind of data. With Popcorn thousands of test cases can be expressed in just few lines of code, which makes it a great driver for data-driven test engines. Let’s go for a quick tour.

First generator (try it):

// Load modules you like to use.
with(Popcorn.Core) {
with(Popcorn.Common) {

  // Define the base test case.
  var toy = {
    name : 'Woody',
    rank : 1,
    type : 'cowboy'
  };

  // Assign generators to attributes you like to alter.
  var generator = {
    name: list('Buzz', 'Slinky')
    rank : range(2, 4)
  };

  // And run it!
  var results = generate(generator, toy);
}

As you would expect, range generates integers from 2 to 4 and lists the names ‘Buzz’ and ‘Slinky’. When executed with generate(), this generator object creates six different result objects representing all combinations between the id and user values.

list() and range() overwrite the values of the base object. append() and prepend() for example use the base object value:

  var p = prepend('Hello ');
  p('Woody'); // returns 'Hello Woody'
  p('Buzz'); // returns 'Hello Buzz', etc.

Some generators accept other generators or generator arrays as arguments. For example append(g) executes the generator g and appends the result to the input value:

  var r = random().int(), // random integer generator.
      a = append(r);
      a('user'); // will return for example 'user42' or 'user573' etc.

If you like to pass a generated value to a generator function that does not accept generators as argument, use chain() function to execute these generators in sequence. For example to build random user names of the form {user}-{random_int} you can define a generator as following:

  var r = random().int(),
      u = chain(r, function(i) { return append('-' + i); });
      u('admin'); // will return for example 'admin-385' or 'admin-712', etc.

chain() executes the generator r and passes the result to the argument function that returns a new generator as result, here the append generator. chain() itself is a generator that can be evaluated or passed to another generator.

The previous example looks quite useful already. We can extract it into a separate library and use it with any other generator, for example:

  var randomUser = chain(random().int(), function(i) { return append('-' + i); });
  repeat(10, randomUser)('user'); // Creates 10 random user names

Popcorn consists of several independently loadable modules that contain generators for random values of different type, dictionaries, network types and many more. For the complete list of generators see API documentation (project repository) and try popcorn maker demos.

We recently released Studio Fx that enables our customers to rapidly do data driven testing of IP services at unprecedented speeds. We extensively use Popcorn in our product to generate these test cases.

Happy hacking!

Posted in JavaScript, Tools | Permalink | Trackback

2 Responses

  1. Twitter Trackbacks for Mu Dynamics Research Labs » Blog Archive » Popcorn - Data Driven Testing with JSON [mudynamics.com] on Topsy.com

    […] Mu Dynamics Research Labs » Blog Archive » Popcorn - Data Driven Testing with JSON labs.mudynamics.com/2009/10/26/popcorn-data-driven-testing-with-json – view page – cached Are you looking for a quick, easy and flexible way to create JSON objects to test your JSON service? Or maybe a tool to mock server responses to test some JavaScript browser code? If so, you might be… (Read more)Are you looking for a quick, easy and flexible way to create JSON objects to test your JSON service? Or maybe a tool to mock server responses to test some JavaScript browser code? If so, you might be interested in Popcorn. (Read less) — From the page […]

  2. Mu Dynamics Research Labs » Blog Archive » Hearing noises in your backyard?

    […] and stitching transactions out of your packet captures. With proven methodologies like Data Driven Testing, you can finally think about testing in terms of spreadsheets, inputs and […]

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.