- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
By using @CADbloke 's excellent CADtest, I've finally managed to get my automated testing going. But I do have a problem with using Transaction in the context of this unit test framework.
The issue here lies with the line tr.Commit():
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
#if (ACAD2006 || ACAD2007 || ACAD2008 || ACAD2009|| ACAD2010|| ACAD2011 || ACAD2012)
using Autodesk.AutoCAD.ApplicationServices;
using Application = Autodesk.AutoCAD.ApplicationServices.Application;
#else
using Autodesk.AutoCAD.ApplicationServices.Core;
using Application = Autodesk.AutoCAD.ApplicationServices.Core.Application;
#endif
using CADtest.Helpers;
using NUnit.Framework;
using System.IO;
namespace CADTestRunner
{
public abstract class BaseTests
{
protected void ExecuteActionDWG(String pDrawingPath, params Action<Database, Transaction>[] pActionList)
{
bool buildDefaultDrawing;
if (String.IsNullOrEmpty(pDrawingPath))
{
buildDefaultDrawing = true;
}
else
{
buildDefaultDrawing = false;
if (!File.Exists(pDrawingPath))
{
Assert.Fail("The file '{0}' doesn't exist", pDrawingPath);
return;
}
}
Exception exceptionToThrown = null;
Document doc = Application.DocumentManager.MdiActiveDocument;
using (doc.LockDocument())
{
using (Database db = new Database(buildDefaultDrawing, false))
{
if (!String.IsNullOrEmpty(pDrawingPath))
db.ReadDwgFile(pDrawingPath, FileOpenMode.OpenForReadAndWriteNoShare, true, null);
using (new WorkingDatabaseSwitcher(db))
{
foreach (var item in pActionList)
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
try
{
item(db, tr);
}
catch (Exception ex)
{
exceptionToThrown = ex;
tr.Commit();//comment it will cause the test runner to hang, if an exception is thrown
//stop execution of actions
break;
}
tr.Commit(); //comment it will cause the test runner to hang
}
}
}
}
}
//throw exception outside of transaction
//Sometimes Autocad crashes when exception throws
if (exceptionToThrown != null)
{
throw exceptionToThrown;
}
}
}
}
And here's the parallel stack trace:
I notice that if you disable all of the test case except the readonly one ( ie: just Ignore all test cases except Test_method_name), and you comment the above tr.Commit(), it should still work because Commit only commits your changes. If you have no changes ( this is what readonly means, right?), then it shouldn't matter whether you commit or not.
But under the test environment, it just hangs at disposing the transaction. Under normal environment where I launch AutoCAD/Civil 3D properly, then commenting the tr.Commit() works just fine.
I have another test case ( which is way more complicated than this one), it also involves only readonly operation, but when disposing the transaction, the code crashes with AccessViolationException instead of hang.
I'm using NUnit 3.13.3/NUnitLite 3.13.3/Civil 3D 2023.1 version. I just don't know whether the problem occurs at AutoCAD/Civil 3D end or NUnit end, someone perhaps can shed some lights on this?
Ngu Soon Hui
##########
I'm the Benevolent Dictator for Life for MiTS Software. Read more here
I also setup Civil WHIZ in order to share what I learnt about Civil 3D
Solved! Go to Solution.