Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

doen't run test with TestCaseSource
Henrry
#1 Posted : Tuesday, June 13, 2017 10:02:42 AM(UTC)
Rank: Member

Groups: Registered
Joined: 1/12/2017(UTC)
Posts: 18
Location: United Kingdom

Thanks: 2 times
Was thanked: 1 time(s) in 1 post(s)
Hello

I have a test like this

[Test]
[TestCaseSource(typeof(LineItemActionTestsSource), "TestCases")]
public bool Should_HasChanges(LineItemAction testValue)
{
var sut = <<create and fill instance>>

return sut.HasChanges(testValue);
}

my test cases yield a complex type of LineItemAction
it runs ok with the default NUnit test runner but NCrunch gives me this warning


This project contains multiple NUnit tests that share the same name. This prevents NCrunch from uniquely identifying tests during their discovery and execution, distorting the reporting of test results.

A common cause of this problem is test cases generated based on a user defined type as a parameter. If you are using user defined types as parameters to your tests, you must implement .ToString() on the user defined type to ensure instances of these parameters can be uniquely represented in test names.

Internally, NUnit uses sequentially generated IDs to identify tests internally and between sessions. Unfortunately, these sequentially generated IDs cannot be used by NCrunch, as the sequence of tests in a suite is continuously being updated while tests are added and removed, and critical state (such as pass/fail, code coverage, output text, etc) must be reliably correlated across multiple versions of a test suite.

There is a high probability that future versions of NUnit will not support test cases without a unique name - https://github.com/nunit/nunit/issues/1336

The following test names are duplicated in this project:

LineItemActionTests.Should_HasChanges([LineItemAction 1])
LineItemActionTests.Should_HasChanges([LineItemAction 1])
LineItemActionTests.Should_HasChanges([LineItemAction 1])
LineItemActionTests.Should_HasChanges([LineItemAction 1])
LineItemActionTests.Should_HasChanges([LineItemAction 1])
LineItemActionTests.Should_HasChanges([LineItemAction 1])
LineItemActionTests.Should_HasChanges([LineItemAction 1])
LineItemActionTests.Should_HasChanges([LineItemAction 1])
LineItemActionTests.Should_HasChanges([LineItemAction 1])
LineItemActionTests.Should_HasChanges([LineItemAction 1])
LineItemActionTests.Should_HasChanges([LineItemAction 1])

I am using VS 2015, NCrunch 3.9.0.1 and nunit 3.2.0.0


Any ideas/sugestions?

Kind regards
Henrry
Remco
#2 Posted : Tuesday, June 13, 2017 10:21:54 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,161

Thanks: 964 times
Was thanked: 1296 time(s) in 1202 post(s)
Hi Henrry,

The warning given by NCrunch here gives an accurate description of the problem. When test cases use a user-defined type, there is no way for NCrunch to tell the tests apart, so you end up with a uniqueness problem. The default NUnit runner doesn't encounter a problem here because in its situation each of the tests is held in memory over the duration of the session, so they are uniquely identifiable via their memory address. Because NCrunch retains the test outside the test process itself, it can't use memory addresses for identification.

The solution is to implement .ToString() on your user defined type so that each of the test cases has a unique and descriptive name.
Henrry
#3 Posted : Tuesday, June 13, 2017 10:45:58 AM(UTC)
Rank: Member

Groups: Registered
Joined: 1/12/2017(UTC)
Posts: 18
Location: United Kingdom

Thanks: 2 times
Was thanked: 1 time(s) in 1 post(s)
That looks like a hack. The entities are equal (that is what i am testing...kind of) so the ToString method should return the same value. Even more, I create a tostring method like this

public override string ToString()
{
DateTime.Now.Ticks.ToString();
}


I am am still facing the same problem

LineItemActionTests.Should_HasChanges(636329509912587830)
LineItemActionTests.Should_HasChanges(636329509912587830)
LineItemActionTests.Should_HasChanges(636329509912587830)
LineItemActionTests.Should_HasChanges(636329509912587830)
LineItemActionTests.Should_HasChanges(636329509912587830)
LineItemActionTests.Should_HasChanges(636329509912587830)
LineItemActionTests.Should_HasChanges(636329509912587830)
LineItemActionTests.Should_HasChanges(636329509912587830)
LineItemActionTests.Should_HasChanges(636329509912587830)
LineItemActionTests.Should_HasChanges(636329509912587830)
LineItemActionTests.Should_HasChanges(636329509912587830)


anything that i can do?
Remco
#4 Posted : Tuesday, June 13, 2017 10:50:03 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,161

Thanks: 964 times
Was thanked: 1296 time(s) in 1202 post(s)
I'd suggest redesigning the test to include the test cases and individual tests within a single [Test]. The user defined type doesn't seem stable enough to be used as a parameter for a test case. Parameters for test cases must be uniquely identifiable and consistent, otherwise NCrunch can't identify the tests or correlate results for them.
Henrry
#5 Posted : Tuesday, June 13, 2017 11:15:44 AM(UTC)
Rank: Member

Groups: Registered
Joined: 1/12/2017(UTC)
Posts: 18
Location: United Kingdom

Thanks: 2 times
Was thanked: 1 time(s) in 1 post(s)
Thanks for the suggestion

Are you aware of any plans in the future to fix this problem?
Remco
#6 Posted : Tuesday, June 13, 2017 11:20:46 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,161

Thanks: 964 times
Was thanked: 1296 time(s) in 1202 post(s)
Henrry;10602 wrote:
Thanks for the suggestion

Are you aware of any plans in the future to fix this problem?


Unfortunately, the problem is caused by a technical limitation. Basically, it's caused by the way that test frameworks work. If NCrunch were only ever to do a single unparalleled run through your test project and never tried to persist any test results or code coverage beyond a single test run, then it might be possible to find a way to make such code work. But this would mean removing all the features that make NCrunch worthwhile, so I don't see this ever being supported by NCrunch or any similar test runner.

I expect that test frameworks will eventually move away from allowing code like this to exist. Unique identification of test cases is becoming a big deal right now.
Henrry
#7 Posted : Tuesday, June 13, 2017 11:24:15 AM(UTC)
Rank: Member

Groups: Registered
Joined: 1/12/2017(UTC)
Posts: 18
Location: United Kingdom

Thanks: 2 times
Was thanked: 1 time(s) in 1 post(s)
Well i guess you can't fix what has no solution :(

thanks anyway
Remco
#8 Posted : Tuesday, June 13, 2017 11:45:07 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,161

Thanks: 964 times
Was thanked: 1296 time(s) in 1202 post(s)
I've just updated the documentation to paint a clearer picture of why this is a problem for NCrunch. It's an interesting coincidence that you posted this support request while I was writing up the documentation page. I guess it shows just how many people are confused by why this is an issue for NCrunch.
Henrry
#9 Posted : Tuesday, June 13, 2017 12:07:41 PM(UTC)
Rank: Member

Groups: Registered
Joined: 1/12/2017(UTC)
Posts: 18
Location: United Kingdom

Thanks: 2 times
Was thanked: 1 time(s) in 1 post(s)
Funny coincidences :)

According to what you say is not NCrunch fault but it would be great having it working. Any way NCrunch still is a really great tool
Remco
#10 Posted : Tuesday, June 13, 2017 12:10:24 PM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,161

Thanks: 964 times
Was thanked: 1296 time(s) in 1202 post(s)
Henrry;10607 wrote:

According to what you say is not NCrunch fault but it would be great having it working. Any way NCrunch still is a really great tool


I appreciate that. I go through great pain to try to support some crazy things with NCrunch, not because I believe in them, but just to try and make life easier for everyone. I've tried very hard to find solutions to this problem. Either the solutions don't exist, or they are far beyond my ability to conceive or implement :(
Henrry
#11 Posted : Tuesday, June 13, 2017 12:12:13 PM(UTC)
Rank: Member

Groups: Registered
Joined: 1/12/2017(UTC)
Posts: 18
Location: United Kingdom

Thanks: 2 times
Was thanked: 1 time(s) in 1 post(s)
if it was an open source software I would say "Can I give a hand?"
Henrry
#12 Posted : Friday, June 16, 2017 3:48:16 PM(UTC)
Rank: Member

Groups: Registered
Joined: 1/12/2017(UTC)
Posts: 18
Location: United Kingdom

Thanks: 2 times
Was thanked: 1 time(s) in 1 post(s)
Interesting...I have another test that is doing something very similar and it is working fine

[Test]
[TestCaseSource(typeof(JobResolutionStatusTestsSource), nameof(JobResolutionStatusTestsSource.StepForward))]
public ResolutionStatus JobResolutionStatusStepForward(Job job, IUserThresholdService userThresholdService, IDateThresholdService dateThresholdService)
{
...
Remco
#13 Posted : Friday, June 16, 2017 11:39:12 PM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,161

Thanks: 964 times
Was thanked: 1296 time(s) in 1202 post(s)
Sorry, I'd need to see the full source of this test and an output of the test names to help give a better understanding of why this test is different.
Henrry
#14 Posted : Saturday, June 17, 2017 7:50:34 PM(UTC)
Rank: Member

Groups: Registered
Joined: 1/12/2017(UTC)
Posts: 18
Location: United Kingdom

Thanks: 2 times
Was thanked: 1 time(s) in 1 post(s)
[TestFixture]
public class JobResolutionStatusStepForwardTests
{
[Test]
[TestCaseSource(typeof(JobResolutionStatusTestsSource), nameof(JobResolutionStatusTestsSource.StepForward))]
[Category("JobResolutionStatus StepForward")]
public ResolutionStatus JobResolutionStatusStepForward(Job job, IUserThresholdService userThresholdService, IDateThresholdService dateThresholdService)
{
var sut = new JobResolutionStatus(userThresholdService, dateThresholdService);

return sut.StepForward(job);
}
}

class JobResolutionStatusTestsSource
{
public static IEnumerable StepForward
{
get
{
yield return new TestCaseData(Imported(), CreateUserThresholdService(true), DateThresholdService(DateTime.Now))
.Returns(ResolutionStatus.DriverCompleted)
.SetDescription("Job should move to DriverCompleted");

//more testcasedata
}
}

private static Job Imported()
{
return JobFactory.New
.With(p => p.ResolutionStatus = ResolutionStatus.Imported)
.Build();
}
}
Remco
#15 Posted : Saturday, June 17, 2017 11:03:08 PM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,161

Thanks: 964 times
Was thanked: 1296 time(s) in 1202 post(s)
Hi Henrry,

This code would work fine provided that there is no more than one test case with the same visible parameter line. I'm not sure if any of these types implement .ToString() to give any kind of variation on the test cases. You'll notice that if you get more than one test case here with the same visible name, NCrunch will only create one test to represent these in the Tests Window and will likely be confused when capturing output from the multiple tests. You'll also see the warning about nunit tests with duplicate names.

In such a situation, you could consider using NUnit's .SetName method to give each of the test cases a name. This might save you from needing to implement .ToString() on types in your production code.
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

YAF | YAF © 2003-2011, Yet Another Forum.NET
This page was generated in 0.078 seconds.
Trial NCrunch
Take NCrunch for a spin
Do your fingers a favour and supercharge your testing workflow
Free Download