开发者

Visual Studio: Test ClassCleanup Timeout When Executing A Batch File

开发者 https://www.devze.com 2022-12-10 16:16 出处:网络
My aim is to restore an Oracle database back to it\'s previous state after a lot of unit tests have been run by using flashback recovery using the ClassCleanup attribute.

My aim is to restore an Oracle database back to it's previous state after a lot of unit tests have been run by using flashback recovery using the ClassCleanup attribute.

I have written a batch file + SQL to properly restore the database. This h开发者_如何转开发as been tested in my command prompt numerous times without issues

I would normally put the restore as a cleanup script inside the test run configuration, however I need to include a time to flashback the database to (set on the ClassInitialize)

        try
        {
            ProcessStartInfo startInfo = new ProcessStartInfo(@"SomeProjectDirectory\flashback.bat");                
            startInfo.CreateNoWindow = false;                
            startInfo.WindowStyle = ProcessWindowStyle.Normal;
            startInfo.ErrorDialog = true;                
            startInfo.Arguments = Arguments;
            startInfo.UseShellExecute = true;                                                

            Process batchExecute = new Process();
            batchExecute.StartInfo = startInfo;
            batchExecute.Start();
            batchExecute.WaitForExit();
        }
        catch (Exception e)
        {
            System.Diagnostics.Debug.WriteLine(e.ToString());
        }

The batch file is executed fine but the problem seems to be that the ClassCleanup method times out before it has finished

It does not hit any breakpoints or catch any exceptions after the WaitForExit(). It simply exits the command prompt window halfway through the process and lets me know all the tests succeeded with no error message.

I tried adding a Timeout attribute to the ClassCleanup method with a large number but this doesn't seem to help either. This attribute I am fairly certain is only applicable to a unit test not the class cleanup

I moved the process into the TestCleanup method and it worked without a problem. The process also ran fine inside a Test. It only seems to timeout in the ClassCleanup & AssemblyCleanup methods

I am under the impression that is a timeout because everytime we run the test, it always drops out at the same point, however if we add or remove steps to the script, it will drop out earlier or later than normal


I've had the same problem. The reason this happens is because of the GC that's going on at the background when ClassCleanup and AssemblyCleanup are being executed. So if you try to access resources outside of the scope of the Cleanup function, more likely than not they'll be garbage collected and the function will quit without throwing an exception.

You can remedy the problem by putting all necessary code in to the Cleanup functions or make local copies of them at the very beginning of the class (which is a bit of a hack). However, this can be tricky with Client-Server applications. You might need to bring in some low-level code in to this function to make this work.

Personally I don't appreciate this design decision, because it makes life much harder than it needs to be.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号