Is it possible to do dynamic tests?

I’m just getting started with testing and having a little issue with trying to create dynamic tests.

Here is an example of what I would like to do:
ValidInputs = [“apple”, “orange”, “banana”];

describe(“Test all valid inputs”, function(){
for(key in ValidInputs) {
it(“Will return true for #key”, function() {
expect(ValidInputs).toInclude(key);
});
}
});

That would hopefully generate the following output:
+Test all valid inputs
Will return true for apple (xx ms)
Will return true for orange (xx ms)
Will return true for banana (xx ms)

but it doesn’t work.

I can get this to work but it does not list out the individual items:
describe(“Test all valid inputs”, function(){
beforeEach(function(){ValidInputs = [“apple”, “orange”, “banana”];});

it(“Will return true for all valid inputs”, function() {
for(key in ValidInputs) {
expect(ValidInputs).toInclude(key);
}
});
});

This is a sort of contrived set of code, but I have a requirement to show a test for a set of values that will be given to me (in this case they will be built into components where I may not know the values).

I’m already expecting the “You’re doing it wrong” statement to come from someone, but I would like to know how to do it right with this requirement.

Thanks,
Steve

Sorry, I didn’t format the subject right. One issue though, when I run the testbox url, it outputs the following:
TestBox v@version.number@+@build.number@

So without looking at the code I’m not sure of the version. I think it is the latest.
Thanks,
Steve

Hi Steven,

Yes this is totally possible. However, you need to pass in the data into the spec closures so they can get binded. If not, you get the results you see above which is not accurate.

Check this out:

http://testbox.ortusbooks.com/content/primers/bdd/specs.html

Spec Data Binding

The data argument can be used to pass in a struct of data into the spec so it can be used later within the body closure. This is great when doing looping and dynamic closure calls:

// Simple Example
it( title="can handle binding", body=function( data ){
    expect(    data.keep ).toBeTrue();
}, data = { keep = true } );

// Complex Example
for (filePath in files) {
    it("#getFileFromPath(filePath)# should be valid JSON", function() {
        var json = fileRead(filePath);
        var isItJson = isJSON(json);
        expect(json).notToBeEmpty();
        expect(isItJson).toBeTrue();
        if (isItJson) {
            var data = deserializeJSON(json);
            if (getFileFromPath(filePath) != "index.json") {
                expect(data).toHaveKey("name");
                expect(data).toHaveKey("type");
            }
        }

    });
}

Well now I know I’m doing something wrong because this worked:

for(myKey in [“apple”, “orange”, “banana”]) {
it(“Will test for #myKey#”, function(){
expect([“apple”,“orange”,“banana”]).toInclude(myKey);
});
}

but the following doesn’t work:

function beforeAll(){ request.inpL = inpL = new com.fruit(); }
function afterAll() { structDelete(request, “inpL”); }

describe(“Test the fruit”, function(){
beforeEach(function(){ myFruit = request.inpL.getFruit();});
afterEach(function(){ structDelete(Variables, “myFruit”);});

it(“Will return an array for getFruit with members”, function(){
expect(myFruit).toBeArray();
expect(arrayLen(myFruit)).toBeGT(0);
}); // This passes

for(myKey in myFruit) {
it(“Will test for #myKey#”, function(){
expect(myFruit).toInclude(myKey);
});
} // THIS ERRORS - Variable myFruit is undefined.

describe(title=“ExtraTest”, body=function(myTest) {
it(“Will return a test”, function(){
expect(myTest).toBeArray();
});
}, myTest=request.inpl.getFruit());
// This also fails inpl is undefined in a Java object of type class [Ljava.lang.String;.

describe(title=“ExtraTest2”, body=function(myTest) {
it(“Will return a test.”, function(){
expect(myTest).toBeArray();
});
}, myTest=myFruit);
// This fails: Variable myFruit is undefined.
});

Sorry about this, but I am starting from scratch here and possibly trying to do things that aren’t possible.

Did you read what I posted. You need to pass the dynamic data into the spec. Via the data attribute

Luis Majano
CEO
Ortus Solutions, Corp
www.ortussolutions.com
P/F: 1-888-557-8057

Luis,

I’m really not trying to be dumb here and I did read your email, I just assumed that “data” was an assignable data name and I used something else. I have tried this using data and it still errors. However if I remove the beforeEach and afterEach and put this inside the describe before the it():
myCom = new com.fruit();
myFruit = myCom.getFruit();

then it works the way shown in your examples. I guess my problem is I was trying to put the component creation into the beforeAll and the call to the function into a beforeEach and they aren’t evaluated as I expected them to.

I still have a lot more to learn. Picking up testing and closures at the same time isn’t easy.

Sorry Steve.

Did not see that either!! Good eye!

Ask away please,