Unexplained Castle and MTM errors - unit-testing

I have a suite that runs a little over 30 tests through MTM. They're selenium tests and take a bit over 20 minutes to run. 6 of these tests are new (2 tests, 3 iterations each) to the project, and error out for the same reason every time they run.
Here's the catch:
1. They pass locally
2. They pass when run individually
The specific error is from Castle ActiveRecord telling me to initialize a class I have most definitely initialized in the code.
[TestMethod]
public void Test(){
Initialize();
//do test
}
public void Initialize(){
if(!ActiveRecordStarter.IsInitialized){
Type[] types = //typeof each castle class;
InPlaceConfigurationSource source = new InPlaceConfigurationSource();
//set up source
...
ActiveRecordStarter.Initialize(source, types);
}
}

MTM runs all the tests without restarting the assembly. If a Castle test runs before my failing tests, it will intialize ActiveRecordStarter, and keep it initialized through my tests. For some reason, my tests did not like this (no answer yet on why), but calling ActiveRecordStarter.ResetInitializationFlag(); before the IsInitialized check fixed the errors.

Related

RestAssuredMockMvc.postProcessors(csrf().asHeader()); doesn't work when multiple tests get executed

I have a RestAssuredMockMvc setup to to unit test my "POST" endpoints. I added
RestAssuredMockMvc.postProcessors(csrf().asHeader());
Since csrf() headers are enabled.
This works fine when I run a test individually (all the tests gets passed, if executed them one by one). But only 1 test at a time gets executed correctly (without returning 403) when all the tests in my class gets executed. If I remove that test, and run all the other tests, another test gets the chance and gets executed correctly without returning 403.
I tried adding the postProcessors like I have mentioned below.
#Before
public void initialiseRestAssuredMockMvcWebApplicationContext() {
RestAssuredMockMvc.basePath = "https://localhost";
RestAssuredMockMvc.webAppContextSetup(webApplicationContext);
RestAssuredMockMvc.postProcessors(csrf().asHeader());
}
& tried adding it under each and every test as well.
I did some debugging in CsrfRequestPostProcessor to check if the get different values for token from TestCsrfTokenRepository for each and every test. Noticed that for the 1st test execution postProcessRequest method gets 1 hit and for 2nd test execution postProcessRequest method gets 2 hits and so on (for 10th unit test postProcessRequestmethod get 10 hits).
Appreciate if someone can help me with this TIA.
I was able to fix it with
#Before
public void initialiseRestAssuredMockMvcWebApplicationContext() {
RestAssuredMockMvc.basePath = "https://localhost";
RestAssuredMockMvc.webAppContextSetup(webApplicationContext);
RestAssuredMockMvc.postProcessors(csrf().asHeader());
}
#After
public void restRestAssured() {
RestAssuredMockMvc.reset();
}
above configurations.
Source - github

Setup reason codes for setupTestcase in Unit Test Framework [AX 2012]

I am using reason code entries in a Dialog form.
For writing the unit Test for the above, I need to first insert reasonCodes dynamically via code in setUpTestCase in UnitTestFramework in Dynamics AX 2012.
How can I do this? I havnt found any help on the internet yet.
Self learned the answer.
In order to write a Unit Test using UnitTestFramework, you create a class which extends SysTestCase class (a System class).
setUp(), setUpTestCase(), tearDown(), tearDownTestCase() are base class functions which are used for setting up and destroying the data during purposely for the test case.
setUp() & tearDown() methods are called at the start & end respectively for each test function in the test case class.
Note, setUp(), tearDown() is run once for every test function while setUpTestCase(), tearDownTestCase() is run only once for for a unit test at the start and end respectively.
Coming back to what I asked,
I had to setup reason codes together with reason comments for writing the test case.
Following is the X++ code required to do so.
private void createReason(str _reasonCode, str _reasonComment)
{
ReasonTable _reasonTable;
_reasonTable.clear();
_reasonTable.Asset = NoYes::Yes;
_reasonTable.Ledger = NoYes::Yes;
_reasonTable.Reason = _reasonCode;
_reasonTable.Description = _reasonComment;
_reasonTable.doInsert();
}
You might have different setting to setup reasons in your test case.
Example, you might wamt to set
_reasonTable.Asset = NoYes::No;
instead of
_reasonTable.Asset = NoYes::Yes;
Call createReason() function in the setUpTestCase() and reasons are inserted into database.
That's all. Hope that it helps someone at some point of time.
Be happy.
~Shakir Shabbir
Have you tried the setUp() and tearDown() methods on the test class?
http://msdn.microsoft.com/EN-US/library/bb496539.aspx
You can create data before the test class execution and delete it when testing ends.

Grails pollution between integration and unit tests

I know there's a lot out there about this particular topic, however I can't quite find anyone who has stumbled across my issue, and hopefully someone can explain this to me.
I have a Domain where I use the injected grailsApplication's dynamic method 'isDomainClass' in the equals method:
#Override
public boolean equals(Object obj) {
if(!grailsApplication.isDomainClass(obj.getClass())) { return false }
...
}
This works fine, and to unit test this i do:
#Mock([MyDomain])
...
def mockGApp
void setUp() {
mockGApp = new Object()
mockGApp.metaClass.isDomainClass = { obj -> true }
}
...
void testSomething() {
def myDomain = new MyDomain()
myDomain.grailsApplication = mockGApp
....
}
And when I run this with test-app -unit (on command line or in STS) it passes just fine.
I then ave an integration test that uses that domain (no mocking this time) and that again runs fine when ran with test-app -integration (either on the command line or in STS)
However if i run 'test-app' so it does both at once, I get a MissingMethodException: no method signature isDomainClass exists with parameters (java.lang.Class) ... and all that jazz.
On investigating it with println's in the service I'm testing in the tests, in the integration portion of the testing, before the equals method of my domain class is called, I can quite happily call grailsApplication.isDomainClass() and get the desired affect. However when the code steps into the domain's equals function the isDomainClass() method no longer exists, despite the grailsApplication object referring to the same object which is referenced in the service and has the dynamically added method.
It appears that the dynamic methods that grails adds to this class are not being injected when its called within the domain's methods but are getting injected within the service. And more bizarrely this only happens if the integration tests follow the unit tests. If done separately, no problemo...
Where does this pollution stem from? IS there any way to solve it?
P.S. using Grails 2.1.0
You have to remove the class you modified from metaClassRegistry in the destroy method (i.e.after test case runs). See below:
#After
void destroy() {
GroovySystem.metaClassRegistry.removeMetaClass(MyDomain.class)
}

Running TestNG test sequentially with time-gap

I have couple of DAO unit test classes that I want to run together using TestNG, however TestNG tries to run them in parallel which results in some rollbacks failing. While I would like to run my Unit Test classes run sequentially, I also want to be able to specify a minimum time that TestNG must wait before it runs the next test. Is this achievable?
P.S. I understand that TestNG can be told to run all the tests in a test class in a SingleThread, I am able to specify the sequence of method calls anyway using groups, so that's not an issue perhaps.
What about a hard dependency between the 2 tests? If you write that:
#Test
public void test1() { ... }
#Test(dependsOnMethods = "test1", alwaysRun = true)
public void test2() { ... }
then test2 will always be run after test1.
Do not forget alwaysRun = true, otherwise if test1 fails, test2 will be skipped!
If you do not want to run your classes in parallel, you need to specify the parallel attribute of your suite as false. By default, it's false. So I would think that it should run sequentially by default, unless you have some change in the way you invoke your tests.
For adding a bit of delay between your classes, you can probably add your delay logic in a method annotated with #AfterClass. AFAIK testng does not have a way to specify that in a testng xml or commandline. There is a timeout attribute but that is more for timing out tests and is not probably what you are looking for.
For adding delay between your tests i.e. test tags in xml, then you can try implementing the ITestListener - onFinish method, wherein you can add your delay code. It is run after every <test>. If a delay is required after every testcase, then implement delay code in IInvokedMethodListener - AfterInvocation() which is run after every test method runs. You would need to specify the listener when you invoke your suite then.
Hope it helps..
Following is what I used in some tests.
First, define utility methods like this:
// make thread sleep a while, so that reduce effect to subsequence operation if any shared resource,
private void delay(long milliseconds) throws InterruptedException {
Thread.sleep(milliseconds);
}
private void delay() throws InterruptedException {
delay(500);
}
Then, call the method inside testing methods, at end or beginning.
e.g
#Test
public void testCopyViaTransfer() throws IOException, InterruptedException {
copyViaTransfer(new File(sourcePath), new File(targetPath));
delay();
}

Resharper running all tests when only a single one is selected

I'm using Resharper 4.5 with Visual Studio 2008 and MBUnit testing, and there seems to be something odd with using ReSharpher to run the tests.
On the side there are the icons beside the class each test method with the options Run and Debug. When I select Run it just shows me the results of the single test. However I noticed that the test was taking a considerably long time to run.
When I ran Sql Server profiler and start stepping through the code, I realized that its not just running the selected test, but every single one in the class. Is there any reason it makes it look like its only running one unit test while actually running them all?
Its getting to be a pain waiting for all integration tests to run when I only care about the reuslt of one, is there any way to change this?
I just encountered this today and I think I might have realized what causes this bug, I had my methods named similarly
[TestMethod]
public void TestSomething()
[TestMethod]
public void TestSomethingPart2()
I saw that running TestSomething() would run both, however running TestSomethingPart2() would not. I concluded if you name methods that an exact match can occur for the method name it will run the test. After renaming my second test to TestPart2Something this issue went away.
I can confirm that this is a problem with ReSharper 5.1.
To reproduce run test A from my sample code below (all tests will execute); run test AB (all except A will execute); etc:
[TestMethod]
public void A()
{
Console.WriteLine("A");
}
[TestMethod]
public void AB()
{
Console.WriteLine("AB");
}
[TestMethod]
public void ABC()
{
Console.WriteLine("ABC");
}
[TestMethod]
public void ABCD()
{
Console.WriteLine("ABCD");
}
[TestMethod]
public void ABCDE()
{
Console.WriteLine("ABCDE");
}
It took me ages to work this out. I had the remote debugger attached to a development server, and it was breaking a bit more often than I was expecting it to...
It seems to be doing a StartsWith instead of a Contains as others have said.
The workaround is to not have test method names that start with the name of another test method name.
I hope this shows up under Chris post.
I had a similar situation that confirms the behavior he noticed.
[TestMethod()]
public void ArchiveAccountTest()
[TestMethod()]
public void ArchiveAccountTestRestore()
So running the first method would execute both and running the second would not. Renamed my second method to TestRestore and the problem went away.
Note: I'm using Resharper 5.1 so it's still a problem.
When you right-click in the editor, the context menu appears from which you can run and debug tests. Right-click inside a test method to run or debug that single test. Right-click outside of any test method to run or debug the entire test class contained in the current file.
The current release of Gallio includes a Unit Test runner with MbUnit (and NUnit) support built-in.
From the Resharper menu, you have the option of running a Single unit test or all Tests in your solution. What is cool, is that the Keyboard-shortcuts for this are:
Alt + R, U, R - Run test from current context (if you are at a [Test] level, it runs one test, if you are at a [TestFixture] level, it runs all in the fixture!)
Alt + R, U, N - Runs all Unit Tests in your Solution
I highly recommend that you uninstall your current Gallio and then check C:\Program Files\Jetbrains\Resharper\plugins\bin and clear out and files there. Then install Gallio afresh.
Once you've done this, you should startup VS2008 and goto at the Resharper | Plugins menu to check that the Gallio plugin is active. This will give you support for MbUnit.