Search code examples
coldfusionmxunit

Order of function execution in mxunit testcase


I'm using MxUnit as a testing framework for my ColdFusion project. But I'm not sure about, in which order the functions in the testcase is getting executed. I've inserted dummy records in DB for my testing, in beforeTests() and deleting those records in afterTests(). I'm having following functions in my testcase

public void function Read() {
//Block of code
}
public void function Save() {
//Block of code
}
public void function Delete() {
//Block of code
}

But at first Delete() gets executed, so the read() returns "No record Found"(failure message), because the record gets deleted in the delete() itself. So I thought that it is running in alphabetical Order and so I changed the function names accordingly(Read(),Save(),XDelete() - since it is in alphabetical order). Again the same thing is happening.

But it works fine, when I renamed the functions as A_Read(),B_save(),C_Delete().

So someone explain about in which order the functions get executed.


Solution

  • You can't guarantee what order they will run in, assume it's random.

    See http://blog.adamcameron.me/2013/11/unit-testing-mxunit-and-test-scenario.html

    For a given test run there is no guarantee what order tests are run in, although in reality it's down to how ColdFusion exposes them in a CFC's metadata, I think. Their execution order is certainly not randomised. But one should not assume any test execution order. And, indeed, each test really needs to be completely discrete from other tests in the CFC

    You really need to rethink how you're doing your tests, so they are all independent of each other.

    However if you really must: http://blog.bittersweetryan.com/2012/01/using-new-orderedtestdecorator-in.html