Rank: Advanced Member
Groups: Registered
Joined: 9/12/2014(UTC) Posts: 155 Location: Netherlands
Thanks: 19 times Was thanked: 11 time(s) in 11 post(s)
|
Here's a repro with C#. Code:
using System.Collections.Generic;
using NUnit.Framework;
namespace SomeClassLib
{
class CSharpTests
{
static IEnumerable<string> source
{
get
{
var temp = System.IO.File.ReadAllLines("packages.config");
return new[] { "foo" };
}
}
[TestCaseSource("source")]
public void SimpleTest(string test)
{
// this succeeds on second run, the file exists!
Assert.AreEqual(System.IO.File.Exists("packages.config"), true);
Assert.AreEqual("foo", test);
}
}
}
To repro, do the following * Create a project, add NuGet reference of NUnit 3.6.1 * Set your packages.config to "Copy if newer" * Wait for NCrunch to run it: error on first run, second run a failing test with normal report This is the error in C#. Same issue with location of file: Code:
System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation.
----> System.IO.FileNotFoundException : Could not find file 'C:\Program Files (x86)\Microsoft Visual Studio\Preview\Enterprise\Common7\IDE\Extensions\Remco Software\NCrunch for Visual Studio 2017\packages.config'.
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index)
at NUnit.Framework.TestCaseSourceAttribute.GetTestCaseSource(IMethodInfo method)
at NUnit.Framework.TestCaseSourceAttribute.GetTestCasesFor(IMethodInfo method)
--FileNotFoundException
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize, Boolean checkHost)
at System.IO.StreamReader..ctor(String path, Encoding encoding)
at System.IO.File.InternalReadAllLines(String path, Encoding encoding)
at System.IO.File.ReadAllLines(String path)
at SomeClassLib.CSharpTests.get_source()
Another repro, which I think shows precisely why I think the cctor is causing the issue (though apparently other static initializers as well): Code:
using System.Collections.Generic;
using NUnit.Framework;
namespace SomeClassLib
{
class CSharpTests
{
static CSharpTests()
{
var temp = System.IO.File.ReadAllLines("packages.config"); // make sure it is set to "Copy if newer" or "Copy always"
}
static IEnumerable<string> source
{
get
{
return new[] { "foo" };
}
}
[TestCaseSource("source")]
public void SimpleTest(string test)
{
// this succeeds on second run, the file exists!
Assert.AreEqual(System.IO.File.Exists("packages.config"), true);
Assert.AreEqual("foo", test);
}
}
}
The latter will throw (only the first time!): Code:OneTimeSetUp: System.TypeInitializationException : The type initializer for 'SomeClassLib.CSharpTests' threw an exception. ----> System.IO.FileNotFoundException : Could not find file 'C:\Program Files (x86)\Microsoft Visual Studio\Preview\Enterprise\Common7\IDE\Extensions\Remco Software\NCrunch for Visual Studio 2017\packages.config'. which may give a better indication on what is going on. As you can see in the F# and C# examples provider, there is absolutely nothing that tries to change the current directory. I also noticed with the latter: - Reload and rebuild will remove the error (all green)
- Run in new task runner process: green
- Reset, or after NCrunch is disabled: red, same OneTimeSetup error (as can be expected)
- Run all tests: error remains (it is not run, apparently in this scenario, "run all" does not run all tests, but I have seen that before, other bug I think)
- Run all visible: error disappears: green
- Run (the button): error disappears: green
- Change code, auto rerun: error reappears
- On startup of VS: error reappears (if it was green last time around)
I'm actually not too surprised that the error disappears, because any code in the static constructor will be run only once for all threads.
|