In many situations this wouldn't be a problem, but it becomes a problem when you are loading your tests from files that are relative to the current dir. In my case it was failing because some initialization code ran a cctor that needed to access a file that was included (and Copy-if-newer) in the project. Once the problem surfaces, it can appear and disappear at will.
Example (in F#, but in C# I'm sure it's the same issue).
type Sources() =
static member source =
System.IO.File.ReadAllLines("Script.fsx")
[<TestFixture>]
type TryTests() =
[<TestCaseSource (typeof<Sources>, "source")>]
member __.TestMe itm =
Assert.AreEqual(true, itm)On most reset and build-after-change this gives, on a select number of the tests (I think depending on how many processes are running them, it will always be the first test it selects) an error similar to this:
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\Scripts.fsx'.
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)
at System.IO.File.OpenRead(String path)
at Tests.NCrunch.Sources.get_sourceNew() in D:\Projects\Experiments\Tests.NCrunch\Tests.fs:line 8It does *not* give this error when doing a "Rebuild this component and rerun all tests". The error also disappears if you simply rerun the test with the error, unless the below error is shown (which seems to be a result of this error, but only appears if there are many tests in the project):
This test was not executed during a planned execution run. Ensure your test project is stable and does not contain issues in initialisation/teardown fixtures.When at some point the error disappears, i.e. after rerunning the test, it won't appear again, as if it remembers where to find the file. Even after Reset or Enable/Disable NCrunch. The same happens in the reverse: if keep trying to rebuild (even with "Run all tests each time"), you keep seeing the error. The only way to let it disappear during a coding session is to rerun the tests (and not change something drastic, like the file location, because then it "forgets" again and gives the same error).
However, I have not managed to get fully rid of the errors by simply rerunning them, unless for very small projects.
There is a workaround for this, but it may be hard for people to find:
TestContext.CurrentContext.TestDirectory + "\\" + relativePath