Unit test ReSharper and NUnit give different results.

Using ReSharper 8.2 on the local computer and NUnit 2.6.3 on the build server, some test was found that passed in ReSharper and failed to run in NUnit. Local and accurate NUnit results are installed, so this is not a difference between computers. Two of my colleagues conducted the same tests and had the same results, so I don’t think it would ruin my computer.

Simplified version of the tests:

[Test] public void Test_UrlQueryString() { var urlInput = "http://www.domain.com/page-with-querystring?url=https://www.domain2.com/page%3Fp%3DPEPE"; var uri = new Uri(urlInput); Assert.AreEqual(urlInput, uri.ToString()); } [Test] public void Test_Dot() { var urlInput = "http://www.domain.com/page-with-dot.?p=google"; var uri = new Uri(urlInput); Assert.AreEqual(urlInput, uri.ToString()); } 

ReSharper's output is all green. Exiting NUnit:

 Runtime Environment - OS Version: Microsoft Windows NT 6.1.7601 Service Pack 1 CLR Version: 4.0.30319.18444 ( Net 4.5 ) ProcessModel: Default DomainUsage: Single Execution Runtime: net-4.5 ...................FF........ Tests run: 29, Errors: 0, Failures: 2, Inconclusive: 0, Time: 0.576769973208475 seconds Not run: 0, Invalid: 0, Ignored: 0, Skipped: 0 Errors and Failures: 1) Test Failure : Test.OrganicTest.Test_Dot Expected string length 45 but was 44. Strings differ at index 35. Expected: "http://www.domain.com/page-with-dot.?p=google" But was: "http://www.domain.com/page-with-dot?p=google" ----------------------------------------------^ 2) Test Failure : Test.OrganicTest.Test_UrlQueryString Expected string length 87 but was 83. Strings differ at index 76. Expected: "...-with-querystring?url=https://www.domain2.com/page%3Fp%3DPEPE" But was: "...-with-querystring?url=https://www.domain2.com/page?p=PEPE" ----------------------------------------------------------------^ 

ReSharper seems to use the same version of NUnit (built-in NUnit 2.6.3)

ReSharper configuration

Does anyone know how to fix this? Is this a bug in ReSharper or NUnit?

+6
source share
1 answer

This is initially a bug in the .NET Framework. The behavior of scaping URIs depends on the environment (version and configuration of the .NET Framework). Bug fixed in .NET Framework 4.5. For example, take the code

 var uri1 = "http://www.domain.com/page-with-dot.?p=google"; var uri2 = "http://www.domain.com/page-with-querystring?url=https://www.domain2.com/page%3Fp%3DPEPE"; Console.WriteLine(new Uri(uri1).ToString()); Console.WriteLine(new Uri(uri2).ToString()); 

Output in .NET Framework 3.5:

 http://www.domain.com/page-with-dot?p=google http://www.domain.com/page-with-querystring?url=https://www.domain2.com/page?p=PEPE 

Output in .NET Framework 4.5:

 http://www.domain.com/page-with-dot.?p=google http://www.domain.com/page-with-querystring?url=https://www.domain2.com/page%3Fp%3DPEPE 

In addition, you can make some changes to the escaping behavior with your .config file. For example, there is an option for .NET 4.0 and a slash:

 <configuration> <uri> <schemeSettings> <add name="http" genericUriParserOptions="DontUnescapePathDotsAndSlashes" /> </schemeSettings> </uri> </configuration> 

I think in your case ReSharper and NUnit just use different configs. Although, it’s really strange that NUnit got the old movie lessons for .NET 4.5. Perhaps NUnit uses some microsoft libraries from .NET 4.0. Alternatively, you can skip the test in ReSharper manually by setting the Framework parameter to CLR4.0. In any case, you should rewrite the tests with environment-independent logic.

Useful links:

+10
source

Source: https://habr.com/ru/post/978517/


All Articles