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

Notification

Icon
Error

Array as datarow attribute parameter
konstantin
#1 Posted : Thursday, July 12, 2018 1:16:06 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 7/12/2018(UTC)
Posts: 2
Location: Russia

Hello,
There is problem in datarow attribute for unit test method. NCrunch can not substitute array parameter into test method.

For example:

[TestMethod]
[DataRow(new string[] { "0" }, false)]
[DataRow(new string[] { "0", "0", "0" }, false)]
[DataRow(new string[] { "0.0001" }, true)]
[DataRow(new string[] { "0", "5" }, true)]
[DataRow(new string[] { "0", "0.0001" }, true)]
[DataRow(new string[] { "0.0001", "0.0001" }, true)]
public void Test1_ExpectSuccess(string[] rateValues, bool expectedResult)
{
...
}

throw an exemption:
Type mismatch on test parameter 1 (System.String[] != System.Object[])

Best regards,
Konstantin
Remco
#2 Posted : Thursday, July 12, 2018 9:25:02 PM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 6,976

Thanks: 931 times
Was thanked: 1257 time(s) in 1170 post(s)
Hi Konstantin,

Do you experience this problem if you set your 'Framework utilisation type for MSTest' to 'DynamicAnalysis'?

DynamicAnalysis provides better support for edge cases like this one.
konstantin
#3 Posted : Friday, July 13, 2018 9:43:33 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 7/12/2018(UTC)
Posts: 2
Location: Russia

Thanks a lot, Remco. It works.
JeffKryzer
#4 Posted : Tuesday, June 11, 2019 6:46:12 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 6/11/2019(UTC)
Posts: 2
Location: United States of America

This appears to still be "broken" with the default installation. Any chance you could change this behavior to succeed with the Static setting? (So I don't have to tell 19 other developers to change a setting, for every test assembly.) Thanks!
Remco
#5 Posted : Wednesday, June 12, 2019 12:01:03 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 6,976

Thanks: 931 times
Was thanked: 1257 time(s) in 1170 post(s)
Hi, thanks for posting.

I'm sorry to say that right now that due to feasibility constraints we have no plans to change how this is implemented. Transferring complex types from an unloaded static/CIL domain into a runtime domain is very tricky business and is full of nasty edge cases, many of which are impossible to handle.

In the current version of the software, when you try to run a test of this structure under StaticAnalysis, you'll receive the following error message:

"Unable to transfer an Array from a statically analysed domain into a dynamic runtime domain. Please change your 'Framework utilisation type for MSTest' setting to 'UseDynamicAnalysis' to run this test."

The correct solution from your side is to change the configuration setting. Using NCrunch's configuration system, you can do this at shared solution level so that all developers on the team will automatically have the setting applied for them when they next update from your VCS.

DynamicAnalysis gives much better support for complex test metadata than StaticAnalysis, because we can discover the tests using the same structure as they will be executed. The current implementation is to enable StaticAnalysis by default (for performance reasons), then provide errors and warnings to encourage moving to DynamicAnalysis for codebases that make use of the edge cases that we can't properly handle using static analysis.
JeffKryzer
#6 Posted : Wednesday, June 12, 2019 4:54:39 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 6/11/2019(UTC)
Posts: 2
Location: United States of America

Thank you for the detailed response. Didn't know about the shared solution configuration. Will pursue.
MrAgitato
#7 Posted : Sunday, October 13, 2019 2:29:58 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 11/23/2017(UTC)
Posts: 5
Location: Sweden

Thanks: 1 times
Was thanked: 4 time(s) in 4 post(s)
I still have a problem with this, even with the DynamicAnalysis setting.

Code example to reproduce the error:
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Tests
{
[TestClass]
public class UnitTest1
{
[DataTestMethod]
[DataRow(1234)]
public void Test(params int[] n)
{
Assert.AreEqual(n[0], 1234);
}
}
}

Works fine in VS and when running 'dotnet test' from command line.
Error message from NCrunch:
"Type mismatch on test parameter 1 (System.Int32[] != System.Int32)"

Software and framework versions:
NCrunch 3.31.0.3
.net core 3.0.100
VS2019 16.3.4
Microsoft.NET.Test.Sdk 16.3.0
MSTest.TestAdapter 2.0.0
MSTest.TestFramework 2.0.0
Remco
#9 Posted : Monday, October 14, 2019 1:18:32 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 6,976

Thanks: 931 times
Was thanked: 1257 time(s) in 1170 post(s)
Thanks for sharing this. I'll take a look and will see if we can get this fixed.
michaelkroes
#8 Posted : Monday, November 11, 2019 2:33:55 PM(UTC)
Rank: NCrunch Developer

Groups: Registered
Joined: 9/22/2017(UTC)
Posts: 280
Location: Netherlands

Thanks: 124 times
Was thanked: 63 time(s) in 60 post(s)
MrAgitato;13946 wrote:
I still have a problem with this, even with the DynamicAnalysis setting.

Code example to reproduce the error:
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Tests
{
[TestClass]
public class UnitTest1
{
[DataTestMethod]
[DataRow(1234)]
public void Test(params int[] n)
{
Assert.AreEqual(n[0], 1234);
}
}
}

Works fine in VS and when running 'dotnet test' from command line.
Error message from NCrunch:
"Type mismatch on test parameter 1 (System.Int32[] != System.Int32)"

Software and framework versions:
NCrunch 3.31.0.3
.net core 3.0.100
VS2019 16.3.4
Microsoft.NET.Test.Sdk 16.3.0
MSTest.TestAdapter 2.0.0
MSTest.TestFramework 2.0.0


Hi,

I've had a look at this. When I run the sample you provided with dotnet test and the VS test explorer, they fail in the same way that NCrunch does.

The Test Explorer throws: System.ArgumentException: Object of type 'System.Int32' cannot be converted to type 'System.Int32[]'.
dotnet test throws the same exception. To me that makes sense since the parameter in the DataRow is an int.


MrAgitato
#10 Posted : Friday, November 15, 2019 9:55:52 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 11/23/2017(UTC)
Posts: 5
Location: Sweden

Thanks: 1 times
Was thanked: 4 time(s) in 4 post(s)
Yes, but the test accepts params int[], which should accept an int as in the code above (this can easily be verified by calling the method from code instead of using attributes).
I discovered that the version of the test adapter affects this behavoir.

Using the following project file you get the failing test behavior (on command line and in VS test runner) that you have observed:

<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="MSTest.TestAdapter" Version="1.4.0" />
<PackageReference Include="MSTest.TestFramework" Version="1.4.0" />
</ItemGroup>

</Project>




However, upgrading the mstest references makes the test run on the command line and in VS test runner but not with NCrunch:
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.0.0" />
<PackageReference Include="MSTest.TestFramework" Version="2.0.0" />
</ItemGroup>

</Project>
1 user thanked MrAgitato for this useful post.
michaelkroes on 12/2/2019(UTC)
michaelkroes
#11 Posted : Saturday, November 16, 2019 5:41:23 PM(UTC)
Rank: NCrunch Developer

Groups: Registered
Joined: 9/22/2017(UTC)
Posts: 280
Location: Netherlands

Thanks: 124 times
Was thanked: 63 time(s) in 60 post(s)
Thanks for pointing that out! I'll have another look :)
michaelkroes
#12 Posted : Monday, December 2, 2019 9:25:32 AM(UTC)
Rank: NCrunch Developer

Groups: Registered
Joined: 9/22/2017(UTC)
Posts: 280
Location: Netherlands

Thanks: 124 times
Was thanked: 63 time(s) in 60 post(s)
1 user thanked michaelkroes for this useful post.
MrAgitato on 12/2/2019(UTC)
MrAgitato
#13 Posted : Monday, December 2, 2019 8:54:28 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 11/23/2017(UTC)
Posts: 5
Location: Sweden

Thanks: 1 times
Was thanked: 4 time(s) in 4 post(s)
Works like a charm, thanks!
1 user thanked MrAgitato for this useful post.
Remco on 12/3/2019(UTC)
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.097 seconds.
Trial NCrunch
Take NCrunch for a spin
Do your fingers a favour and supercharge your testing workflow
Free Download