Posted 07 September 2013 - 05:52 PM
Big Twisty’s Software Validation API
Download: http://pastebin.com/b585vtx3
Background:
Software Validation is a process of using a predefined group of tests to prove functionality of a piece of software. Have you ever had a perfectly working piece of code, but you found some way to make it better? How do you know you didn’t break something else in that massive operating system you’re putting together? This is where software validation comes in. It allows you to quickly and easily go back and test all the functionality that you previously proved out after any major (or minor) software change.
The typical development process, at least for me when I’m lazy, is this:
API Features:
Let’s write a quick piece of code to test. Credit goes to theoriginalbit for the original version of this override for the assert() function. I’ve modified it a bit for my purposes, but the idea was his.
Now let’s setup a new test.
This sets up a new test that will output all test data to “output.txt”. We’ll see what pline does later.
Now let’s run a small test on it.
The first line adds a note to the test, so we know what this section is testing.
The test.try() function is declared like this:
tag: gives the test a reference, usually a number, so you know which test you’re looking at in the report.
testCall: A table containing the function and parameters you want run.
results: A table containing the expected results. The first item is a Boolean representing whether or not you expect the function succeed. If you don’t, the second item is the expected error message. If you do expect it to succeed, the remaining items in the expected table should contain the expected return values for the function.
Normally a well designed application will not throw errors referenced inside the application. Expected errors should “throwback” to the caller. In this case, the caller is always “pcall”, hence the expected error message for test 1.1. (see how handy those tags are?)
Notice that when using assert(bool, msg, 2) the throwback goes back one level further than pcall. The btValidation.pcallLine() function returns the expected error code prefix for the line in the API where the pcall occurs for just this purpose. It may only really be useful for testing error handling code, but it’s there anyway.
Feel free to play around with this if you like. If not, feel free to kick your dog or whatever…
BigTwisty out…
Download: http://pastebin.com/b585vtx3
Background:
Software Validation is a process of using a predefined group of tests to prove functionality of a piece of software. Have you ever had a perfectly working piece of code, but you found some way to make it better? How do you know you didn’t break something else in that massive operating system you’re putting together? This is where software validation comes in. It allows you to quickly and easily go back and test all the functionality that you previously proved out after any major (or minor) software change.
The typical development process, at least for me when I’m lazy, is this:
- Write a new piece of cool code that does FOO.
- Make sure it does FOO right.
- Notice it could also, with a bit of tweaking, do BAR as well.
- Tweak it (not twerk it, much different!)
- Make sure it now does BAR.
- Drink a beer.
- Use the code in a bunch of other stuff.
- Try to figure out why my other stuff isn’t working, not realizing that my cool code may do BAR now, but FOO got broke and I didn’t know it!
- Bang my head on a wall…
- Kick my dog…
- Write a darn Software Validation API so I don’t do it again!
- Write a new piece of cool code that does FOO.
- Start a software validation script for the code
- Add a step that tests FOO.
- …add BAR… add BAR test step…
- Run validation test and see that FOO doesn’t work.
- Fix it, drink beer, pet dog, smile at wife…
API Features:
- Test generation
- Each test is tagged for reference
- Failed tests include expected vs received data
- Error reporting support
- Can test your code’s ability to throw errors when expected
- Test continues even when errors are thrown
- Can print failed tests with data to the screen
Spoiler
Let’s write a quick piece of code to test. Credit goes to theoriginalbit for the original version of this override for the assert() function. I’ve modified it a bit for my purposes, but the idea was his.
function assert(bool, message, throwback)
throwback = throwback == 0 and 0 or throwback and (throwback + 1) or 2
if not bool then
error(message, throwback)
end
end
Now let’s setup a new test.
os.loadAPI("btValidation")
test = btValidation.newTest( "output.txt" )
pline = btValidation.pcallLine()
This sets up a new test that will output all test data to “output.txt”. We’ll see what pline does later.
Now let’s run a small test on it.
test.note("assert")
test.try( "1.0", { assert, true, "test" }, { true } )
test.try( "1.1", { assert, false, "test" }, { false, "pcall: test" } )
test.try( "1.2", { assert, false, "test", 2}, { false, pline.."test" } )
The first line adds a note to the test, so we know what this section is testing.
The test.try() function is declared like this:
try = function( tag, testCall, expected )
tag: gives the test a reference, usually a number, so you know which test you’re looking at in the report.
testCall: A table containing the function and parameters you want run.
results: A table containing the expected results. The first item is a Boolean representing whether or not you expect the function succeed. If you don’t, the second item is the expected error message. If you do expect it to succeed, the remaining items in the expected table should contain the expected return values for the function.
Normally a well designed application will not throw errors referenced inside the application. Expected errors should “throwback” to the caller. In this case, the caller is always “pcall”, hence the expected error message for test 1.1. (see how handy those tags are?)
Notice that when using assert(bool, msg, 2) the throwback goes back one level further than pcall. The btValidation.pcallLine() function returns the expected error code prefix for the line in the API where the pcall occurs for just this purpose. It may only really be useful for testing error handling code, but it’s there anyway.
Feel free to play around with this if you like. If not, feel free to kick your dog or whatever…
BigTwisty out…