In wz code
Test.run (test1)
test1 is nameof test function
function test1()
function setup
function tearDown
function testThat
function dosomthing
For each function that starts with test you call setup function before and teardown.
Test function can contain many assertation and other things. If assert fails it just raise exception.
It is style of junit of java and unittest in python.
Each test class or test method can be launched separetly.
Re: Unit testing
Posted: 28 Nov 2012, 05:59
by aubergine
Ok, I can probably implement something like that - do you have any links to examples of that style of test script?
Note the order of expected/actual params on all assertions has been changed to actual, expected (was expected, actual) -- if you've made any test scripts using this API, please update them. Sorry for the pain, but it's to keep the assertions in same format as QUnit where possible.
With the similarTo() and notSimilarTo() assertions, I've added some constants: Test.FOUND and Test.NOT_FOUND as detailed in the docs:
With regard to using the constants on the notSimilarTo() assertion, I'm keen to get feedback as to whether they "sound right" when used in that context?
For example, these tests will pass because actual value is not similar to expected value:
I wasn't sure whether to invert the meaning of the constants when used in notSimilarTo() -- any comments?
I've left similarTo() and notSimilarTo() marked as "beta" in the docs for now, they are both tested and work, but I want to see if anyone has any better ideas about the constants before treating them as final.
I have been thinking about making a unit testing map with its own javascript that would run various calls automatically, and then ask the user to do various things to trigger things that cannot easily be tested automatically. This map could be added to the distribution and be run as part of the commit changes to scripting / official release procedure.
If anyone want to have a go at this, it would be nice. I can add a hackAssert(condition, message...) function for it.
Re: Unit testing
Posted: 08 Dec 2012, 12:55
by aubergine
Could that not be done with a challenge map and do the unit testing in JS?
What would the hackAssert() function do?
EDIT: BTW, I've been pondering something similar, perhaps even using audio prompts and console messages to guide the user through the testing process. I was also thinking similar technique could be used to create a series of tutorial maps...
I was on vacations. mobile device is not good to inspect code.
I write some code as example of that I like.
Big peace of code just insert it to js test. (it can be run in any js enviroment just change "console" to other output.) (dont try to read it, coffescript interpretator writes cod not for reading )
// test class can be writted in more pretty way
SimpleTest = (function(_super) {
__extends(SimpleTest, _super);
function SimpleTest() {
return SimpleTest.__super__.constructor.apply(this, arguments);
}
// test method. It can be more pretty too, return is not needed. It exception catched test failed or errored, else passed
SimpleTest.prototype.testFoo = function() {
// you can put any code and any number of assertation to here.
return this.assert(1);
};
SimpleTest.prototype.testNegative = function() {
return this.assert(0);
};
return SimpleTest;
})(TestCase);
// this part should be integrated. For example search all instances of TestCase in code and use them.
test = new SimpleTest('zzzzz');
res = new Runner([test]);
// running tests
res.run();
// printing test output
console(res.getResultText());
assertationKeyword = 'AssertationError:'
class Runner
constructor: (@tests) ->
@result = []
run: () ->
for test in @tests
@result.push test.run()
getResultText: () ->
testResult = []
for res in @result
for text in res
testResult.push("#{text.class}.#{text.test} #{text.result} #{text.error}")
testResult.join('\n')
toString: ()->
'Testrunner'
class Assertation
assert: (val) ->
if !val
throw "#{assertationKeyword} assertation of #{val} fail"
class TestCase extends Assertation
constructor: (@_name='unnamed test')->
@_tests = []
for key, value of @
if key[..3] == 'test'
@_tests.push(key)
@result = []
preTest: ->
postTest: ->
run: () ->
for key in @_tests
@preTest()
@result.push @doTest(key)
@postTest()
return @result
doTest: (key) ->
try
@[key]()
catch error
msg = error.toString()
if msg[..assertationKeyword.length-1] == assertationKeyword
return {'result': 'fail', 'class': @_name, 'test': key, 'error': msg[assertationKeyword.length..] }
else
return {'result': 'error', 'class': @_name, 'test': key, 'error': msg }
return {'result': 'pass', 'class': @_name, 'test': key, 'error': '' }
class SimpleTest extends TestCase
testFoo: () ->
@assert 1
testNegative: () ->
@assert 0
test = new SimpleTest('zzzzz')
res = new Runner([test])
res.run()
console res.getResultText()
Re: Unit testing
Posted: 09 Dec 2012, 01:08
by aubergine
And what would happen if you had lots of test scripts and wanted to split them out in to separate .js files? Variables like __extends, etc., would need to be put on to the global scope, unless you propose that all tests are put in to one massive .js file?
Anyway, I can see the type of syntax & extensibility you are looking for and will add it to my list of future updates to my Test API. My current focus is just on getting more of my APIs released with testing under the current Test API framework.
Re: Unit testing
Posted: 09 Dec 2012, 07:31
by Duha
aubergine wrote:And what would happen if you had lots of test scripts and wanted to split them out in to separate .js files? Variables like __extends, etc., would need to be put on to the global scope, unless you propose that all tests are put in to one massive .js file?
You just need to import all test classes to you scope.
I this is manual way, but it is should not be used.