Jump to content
pyscripter

DUnitX and StackTraces

Recommended Posts

I am new to DUnitX and I am trying to get stacktraces working, so that when a test case fails, I can see the source code line containing the Assertion that failed.  I have done the following:

  • Added DUnitX.StackTrace.Jcl, to the project uses clause.
  • Edited C:\Program Files (x86)\Embarcadero\Studio\20.0\source\DunitX\DUnitX.Stacktrace.inc to enable the JCL define
  • Compiled generating a full MAP file which was converted to jdbg and inserted into the executable.  The MAP file is also present.

I still do not get stack traces for failures.  What am I missing?

 

Another small issue is the XML report shows

asserts="0"

despite the fact that there are many Assert.IsTrue in the Test cases.  Is this feature not working?

Share this post


Link to post

Did you get this working

 

I have tried the MadExcept option, and it is getting called, but the stack trace is not being shown.

 

Dave

Share this post


Link to post

Don't want to be a party pooper, but I never saw the reason for having detailed stack traces. Tests should fail for exactly one reason. If you have a bunch of entirely different checks in your test case and struggle to find out what exactly failed, there's something wrong. Better split them up in seperate tests.

 

I am still on DUnit and have never really used DUnitX, but can't you debug a test? That should easily tell you what, and where something is going wrong. The fabulous "Test Insight" plugin from Stefan allows you to directly debug the test at your cursor position with [Alt]+[F9]

sglienke / TestInsight / wiki / Home — Bitbucket

  • Thanks 1

Share this post


Link to post
10 hours ago, Dave Craggs said:

Anyone know how to view the stack trace using the GUI test runner?

It looks like this was never implemented. I have never used the gui test runner - I prefer TestInsight - but it should be relatively simple to implement - a quick look suggests it could be added in TGUIVCLTestRunner.ProcessMessagesForNode - follow the pattern use for messages.

 

Happy to accept a PR for this 😉 

Share this post


Link to post
On 2/18/2020 at 2:55 PM, pyscripter said:

I still do not get stack traces for failures.  What am I missing?

Sorry I didn't see this thread back when it was originally posted. I have never used the JCL stack trace provide - and github seems to have lost the history for the file since I re-orged the repo layout - so not sure who contributed it (was a long time ago). I have used the madExcept one in the past, but generally only rare occasions when a test fails on the CI server but not on my machine. You do appear to have performed the correct steps to enable it. The stack trace is currently only reported in the NUnit and JUnit loggers. 

 

On 2/18/2020 at 2:55 PM, pyscripter said:

despite the fact that there are many Assert.IsTrue in the Test cases.  Is this feature not working?

The feature isn't implemented - the value is hard coded in the output - probably to satisfy a parser somewhere. While DUnitX does count the asserts per test, it doesn't currently have a way of reporting the total number of asserts. If you are still needing this it would be worth logging an issue on github so it doesn't get forgotten.

  • Thanks 1

Share this post


Link to post

OK - first, I use the GUI Test Runner when developing tests. Tests are then run using the console tester when doing builds using FinalBuilder.

 

I need the stack trace to tell me where exceptions occur when tests fail.

 

I have managed by using Codesite to show the stack trace, that will do for now.

Share this post


Link to post
2 hours ago, Dave Craggs said:

I use the GUI Test Runner when developing tests.

You should really give TestInsight a try - it will make developing tests so much easier.

 

3 hours ago, Dave Craggs said:

I need the stack trace to tell me where exceptions occur when tests fail.

Are you sure about that? Unless something on the CI system causes the tests to fail which is not the case locally you will see the exact spot when running them locally.

 

FWIW DUnit has had support for stack trace collecting via either Jcl or madExcept for ages (enable via defines).

 

Share this post


Link to post

I just looked in my own unit tests and I can see that I have added the following to enable madExcept support. I can't remember why - or even if it works.

...unrelated stuff snipped...

// Work around for broken Delphi 10.3 compiler support in bundled DUnitX
{$if defined(MADEXCEPT)}
type
  TMadExcept4StackTraceProvider = class(TInterfacedObject, IStacktraceProvider)
  public
    function GetStackTrace(const ex: Exception; const exAddressAddress: Pointer): string;
    function PointerToLocationInfo(const Addrs: Pointer): string;
    function PointerToAddressInfo(Addrs: Pointer): string;
  end;

function TMadExcept4StackTraceProvider.GetStackTrace(const ex: Exception; const exAddressAddress: Pointer): string;
begin
  Result := madStackTrace.StackTrace(false, false, false, nil, nil,
    exAddressAddress, false,
    false, 0, 0, nil,
    @exAddressAddress);
end;

function TMadExcept4StackTraceProvider.PointerToAddressInfo(Addrs: Pointer): string;
begin
  Result := String(StackAddrToStr(Addrs));
end;

function TMadExcept4StackTraceProvider.PointerToLocationInfo(const Addrs: Pointer): string;
begin
  Result := String(StackAddrToStr(Addrs));
end;
{$ifend}

begin
{$if defined(MADEXCEPT)}
  TDUnitXIoC.DefaultContainer.RegisterType<IStacktraceProvider, TMadExcept4StackTraceProvider>(true);
{$ifend}

{$if defined(RUNNER_TESTINSIGHT)}
  ExecuteTestInsight;
{$elseif defined(RUNNER_GUI)}
  ExecuteGUIRunner;
{$else}
  ExecuteConsoleRunner;
{$ifend}
end.

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×