DSL for test data generation

There are several ways to generate data for tests (not only unit tests), for example, Object Mother, collectors, etc. Another useful approach is to write test data in plain text:

product: Main; prices: 145, 255; Expire: 10-Apr-2011; qty: 2; includes: Sub
product: Sub; prices: 145, 255; Expire: 10-Apr-2011; qty: 2

and then parse it into C # objects. It is easy to use in unit tests (since deep internal collections can be written on one line), it is even more convenient to use in a FitNesse-like system (because this DSL fits naturally into a wiki), etc.

So, I use this and write a parser, but it is tedious to write each time. I am not a great expert in DSL / language parsers, but I think they can help here. What would be the right option? I only heard about:

  • DSL (I mean, any DSL)
  • Boo (which I think DSL can do)
  • ANTLR

but I don’t even know which one to choose and where to start.

So the question is: is it wise to use some kind of DSL to generate test data? What would you suggest to do? Are there any existing cases?

Update: it looks like I was not clear enough. This is not about converting the source string to an object. Look at the first line and bind it to

var main = Product.New("Main")
   .AddPrice(Price.New(145).WithType(PriceType.Main).AndQty(2))
   .AddPrice(Price.New(255).WithType(PriceType.Maintenance).AndQty(2))
   .Expiration(new DateTime(10, 04, 2011));
var sub =  Product
   .New("Sub").Parent(main)
   .AddPrice(...));
main.AddSubProduct(sub);
products.Add(main);
products.Add(sub);

And note that I first create an additional product, and then add it to main, even if it is listed in reverse order. Prices are handled in a special way. I want to specify the name of the Sub-product and get a link to it - created. I want to list all the product properties - FLAT and NON REPEATATIVE - on one line. I want to use default values ​​for properties. Etc.

: , DSL, . DSL.

+5
3

DSL YAML . :

---
receipt:     Oz-Ware Purchase Invoice
date:        2007-08-06
customer:
    given:   Dorothy
    family:  Gale

items:
    - part_no:   A4786
      descrip:   Water Bucket (Filled)
      price:     1.47
      quantity:  4

    - part_no:   E1628
      descrip:   High Heeled "Ruby" Slippers
      price:     100.27
      quantity:  1

bill-to:  &id001
    street: |
            123 Tornado Alley
            Suite 16
    city:   East Westville
    state:  KS

ship-to:  *id001

specialDelivery:  >
    Follow the Yellow Brick
    Road to the Emerald City.
    Pay no attention to the
    man behind the curtain.

YAML ​​ .

, , "" . , (), , . , , .

YAML .

+2

, , DSL. # :

Product[] products = new Product[] {
    new TestProduct{product="Main", prices=new[]{145, 255}, Expire="10-Apr-2011", qty=2, includes="Sub"},
    new TestProduct{product="Sub", prices=new[]{145, 255}, Expire="10-Apr-2011", qty=2}
};

, , , , DSL.

, Expire , , , . DSL, TestProduct.Expire setter .

+1

DSL Eclipse TMF Xtext, ( ANTLR, ), Eclipse Java, . , , Ruby on Rails, YAML, , , . Railscasts 158: , DSL.

0
source

All Articles