Has anyone ever run into an issue with .net 4.8 (c#) throwing "too long path" errors like below when run from a class library project. The same line of code is working fine for me if I run it from a windows forms project.
the error is below
"The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters"
The code I'm using to get the error is pretty simple. StrPath is a valid path that is say 350 chars long. ("c:\<350 Char path and filename>")
string a = Path.GetDirectoryName(strPath);
That's the simplest example, but it also happens when using any File or FileIO method like File and FileStream where I reference the long path.
Things I've tried
Most recommendations from https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file?redirectedfrom=MSDN
setting the long path registry key
Including app manifest with settings to recognize long paths. (there were two different types of these I found on the net, neither worked)
prefixing with "\\?\"
Looked at what mscorlibs are being referenced by both the windows forms and the exe calling the class library dll. Both were identical and referenced 4.8 libs.
Both projects succeed when I use a shorter path than 260. So it is really a path len issue I think. It just seems to behave as if I'm using an older version of .net when run from the class library.
I call the class library from within a native C++ app (its a com interop). I can also get it to happen if I make a class library with that code into an nunit text fixture and call it from nunit-x86.exe. So the code would be as below for that. And fails when calling GetDirectoryName
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using Omtool.Properties;
using System.Diagnostics;
using System.IO;
using OmXceedInterop;
using System.Windows.Forms;
namespace XceedTest
{
[TestFixture]
public class Class1
{
[Test]
public void Test()
{
string strPath = "C:\\Kit\\BlahServer\\Work\\CompoundFile\\Xceed\\2a0bb008-397b-476a-8e89-96caa7a11319\\HLCUVL1221104974\\THIS_NAME_IS_TOO_FRACKING_LONG_TO_BE_IN_ANY_WAY_USEFUL_YOU_SHOULD_BE_A_FRACKING_SHAMED_OF_YOURSELF_HLCUVL1221104974_Container Inspection_NANDA TILES,SL-DOCUMENTO CONTENEDOR-NEXP-220199-STYLE ACCESS-FLOOR DECOR-BAYTOWN TX-NO.1001367035.pdf";
int b = strPath.Length;
string a = Path.GetDirectoryName(strPath);
}
}
}
Again, the exact same lines of code work from the forms app but fail when called from the class library. There is something different about these two projects or perhaps the context under which they run that I'm just not understanding.
[EDIT]
It has to be the context. When I run my nunit class from the forms app, there is no path error. When I run that same assembly from nunit-x86.exe, I get the pathing error. It seems to depend on what's calling the class library as to whether I get the error or not
The setup is as follows:
a .NET Standard 2.0 library
a NUnit test project for it, targeting .NETFramework 4.6.1
Latest VS 2017, latest NUnit.
I've been working on the project on weekend from home, uploaded my work to git and today started working from work (I've already worked from both places before). Only now I found that something was wrong with the project (I don't remember very well what was wrong at the start, but it seems the problem was the same as I have now, described later).
After fiddling around to unrepairable state with it, I wholly deleted it and cloned the git repo anew.
The project compiles fine, but at runtime tests throw "Method not found" exception. A bit of poking around showed that the problem only manifests on one overload of the following method:
public static YNABClient GetInstance(HttpMessageHandler _handler)
{
if (instance is null)
{
instance = new YNABClient(_handler);
}
return instance;
}
public static YNABClient GetInstance() => GetInstance(new HttpClientHandler());
The one without parameters is fine, the one with is not. Deleting and adding library as a reference to tests, deleting and adding both test and library project. Other solutions for similar situations I found on the internet all pertain to ASP.NET MVC, which is not my case, though this question did lead me to checking overloads and finding that one of them actually works.
At home everything still works fine, though I have yet to try to delete and reinstall the project as I did at work. This leads to 2 possible sources for problems: environment, though I haven't managed to find a meaningful difference, or git, though I use a "stock" git ignore for VS (this one), so there shouldn't be problems there. The basic setup for my projects didn't change during weekend and worked before, so something broke from recent fiddling.
Also, if I add a console application(.Net Framework 4.6.1) to solution and try calling the problematic method from it, it actually works fine.
If that would help, my github for the project is here
I've been asked for calling examples in the comments. Basically, I have 2 Test Fixture classes with different setups - one for real API calling for ease of debugging actual use, and one with faking it, as per good test practices.
Works:
[OneTimeSetUp]
public void Setup()
{
ynabClient = YNABClient.GetInstance();
ynabClient.RefreshAccessToken(ApiKeys.AccessToken);
}
Throws exception:
[OneTimeSetUp]
public void Setup()
{
handler = new StubHandler();
ynabClient = YNABClient.GetInstance(handler);
}
Some poking around shows that my problem is quite likely related to System.Net.Http versioning discrepancy with .NET Framework and .NET Standard, that is quite a widespread problem if you google it. However none of the examples I dug up exhibit my particular symptoms, and I'm not yet sure what to do. And why everything works fine on my home PC.
The issue that you have is that your GetInstance method accepts HttpMessageHandler as parameter, and in your test you are passing HttpClientHandler object. So, you have declared one parameter, but provide different object when you call the method.
I got this error in my environment:
Severity Code Description Project File Line Suppression State
Error CS0433 The type 'HttpMessageHandler' exists in both
'System.Net.Http, Version=4.2.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a' and 'System.Net.Http,
Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
Your YNABConnector is .NET Standard 2 but Unit test is 4.6.1, they are using different signature for same assembly.
Here is official document to test:
https://github.com/nunit/docs/wiki/.NET-Core-and-.NET-Standard
created a .NET Core Library(be aware that: cannot be .NET Standard library by above link),
copy your test code there,
follow the official document to add references,
your code works then, no errors.
But in your base code, I still prefer this code:
public class YNABClient
{
private static YNABClient instance;
private HttpClient client;
private HttpMessageHandler handler;
private YNABClient(HttpMessageHandler _handler = null)
{
handler = _handler ?? new HttpClientHandler();
client = new HttpClient(_handler);
}
public static YNABClient GetInstance(HttpMessageHandler _handler = null)
{
return instance ?? (instance = new YNABClient(_handler));
}
......
}
So, it turns out, as DongDong suggested, the problem is indeed with interfacing between .NET Framework and .NET Standard. The problem is not really that they're incompatible, or that Test project needed some additional dependencies, but that they're shipped with different versions of System.Net.Http.
The diagnostics were hindered by showing no visible errors. However, changing parameter types showed that indeed, the problem is only with classes from that namespace.
Another problem with diagnosing the issue was that the project works fine on some machines (my home PC and Daisy Shipton's from comments to the question).
However after determining the source of the problem I was able to google what problems exist with the library in the first place, and eventually found a trove of uncompatibility issues on .NET github.
I tried the solution used in those cases and added a "binding redirect" to a concrete version of the library, and after that it works fine. I added the redirect to app.config of my Tests project. For reference it looks like this:
<dependentAssembly>
<assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.2.0.0" />
</dependentAssembly>
I still have no understanding of why the project worked fine on some machines.
I'am totally new to Windows Store App programming,
so i'am also new to Visual Studio Express.
My goal is to test a simpple class method.
As mentioned here the Express version do not have any built-in Unit testing.
In this thread, Rafal provides a solution.
I exactly did it like described, so my external tools look like this:
When i execute it (Tools --> "Execute NUnit"), Nunit starts and the Gui of NUnit appears. But suddenly this exception occurs:
And in Exception Details:
System.IO.FileNotFoundException...
at NUnit.Util.ProjectService.WrapAssembly(String assemblyPath)
at NUnit.Util.ProjectService.ConvertFrom(String path)
at NUnit.Util.ProjectService.LoadProject(String path)
at NUnit.Util.TestLoader.LoadProject(String filePath, String configName)
My project folder has this structure:
The test classes are in "WebTest.Shared".
I think i need a .dll to run in NUnit as mentioned by Jon here.
So, how can I make a dll out of my project to run it with NUnit?
Can anyone guide me through this problem? (Please step by step)
EDIT:
After i worked in ChrisM idea, the exception stll arises without "${BinDir}${TargetName}.dll/run" block (the exception details are the same as before):
EDIT No. 2:
I have set those values:
Title: Execute NUnit
Command: D:\Path\To\NUnit\bin\nunit.exe
Arguments: $(BinDir)$(TargetDir)$(TargetExt)/run
Initial directory: $(BinDir)
EDIT No. 3:
After closing and reopening VS Express
i got this new Exception:
And in NUnit Exception Details:
System.ApplicationException: Unable to find test in assembly
System.ApplicationException...
EDIT No. 4
Here is my test class (StringUtilitiesTest.cs):
using System;
using System.Collections.Generic;
using System.Text;
using WebappTest.Shared.Utilities;
using NUnit.Framework;
namespace WebappTest.UnitTest
{
[TestFixture]
public class StringUtilitiesTest
{
[Test]
public void TransferFunds()
{
Assert.AreEqual("Hello", StringUtilites.getString("Hello"));
}
}
}
In external Tools:
Have you tried replacing the curly braces {} in the argument box with normal ones ()?
Visual Studio 2017 Express (the final express version) includes the test explorer. Add the NUnit3TestAdapter NuGet to your project, and the test explorer should discover your tests.
I Developed a small Application in C#. I want to test my application with NUnit.I am a new to NUnit.I Installed NUnit but don't Know How to use it what are the basic steps needed for it or please provide me a good reference link about using NUnit.
Check out the NUnit quick start:
Let’s start with a simple example.
Suppose we are writing a bank
application and we have a basic domain
class – Account. Account supports
operations to deposit, withdraw, and
transfer funds.
I recommend you to have an own project for your tests (like Project.Tests).
Place the following basic files somewhere in folder of your project structure (e.g. lib\nunit\nunit):
nunit.core.dll
nunit.core.interfaces.dll
nunit.framework.dll
nunit.util.dll
nunit-console.exe
nunit-console.exe.config
nunit-console-runner.dll
nunit-console-x86.exe
nunit-console-x86.exe.config
Then you need to reference the NUnit.Framework assembly in your Project.Tests project.
For example, a simple test would look like this:
using NUnit.Framework;
namespace Project.Tests
{
[TestFixture]
public class MyTestClass
{
[Test]
public void MyTestMethod()
{
var a = "a";
var b = "a";
Assert.AreEqual(a, b);
}
}
}
You can run this test then for example with the NUnit-console or directly in VisualStudio (e.g. with the help of ReSharper) or through a MSBuild task with the help of MSBuild Community Tasks.
If you don't use resharper I recommend you to use this plugin - http://www.testdriven.net/ .
I have a c# solution with the following structure:
mySolution
myProject
myProject.MSTests
References
Microsoft.VisualStudio.QualityTools.UnitTestFramework
sutMSTests.cs
sutMSTests.cs:
[TestClass()]
public class sutMSTests
{
[TestMethod]
public void MyTest0()
{
Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(4, 2 + 2);
}
}
When I try to run the tests via Test, Run, All Tests In Solution, I get the following on the VS2008 status line:
No tests are run because no tests are loaded or the selected tests are disabled.
Test, Windows, Test View shows no tests.
Note: I created the tests manually (works for xUnit.net) instead of using Microsoft's wizards.
I've compared my hand created MSTest setup to the setup another test that I generated using the wizard and they appear to be sufficiently similar.
Question: What are the most likely causes of the error message above?
Edit 2010-02-25: More information:
I right clicked the Solution Items folder, and choose Add, New Project, type Test Projects,Test Documents::Visual Studio Test Project template.
The new project's default do nothing test "TestMethod1" was detected and passed.
However, my test did not show up ... so I copied and pasted my test method into the default test test project "TestProject1".
My test was detected in "TestProject" BUT not in its original location.
I closely compared the files, organization, and settings of "TestProject1" with my hand created test project.
At this point, I am guessing that some setting gets made by the Visual Studio Test Project template that is not easily detectable.
imo, it should be just as easy to create a test project by hand as it is to create one with the Visual Studio Test Project template.
please note: I'm not saying that I'm against using the Visual Studio Test Project template; for me, I like to understand what's behind the curtain since this makes me imho a much better programmer.
Another one for the googlers - this one turned out to be my problem, and it's embarrassingly boneheaded of me. Make sure that your test project is set to build in whatever solution configuration you're using. If the test assembly isn't being built, VS won't be able to find any tests in the non-existent assembly, and you'll bang your head against the wall for a while :-)
Possibly a bit late, but this question googles up well, I thought I'd throw some crumbs in for future googlers.
Bryan Cook suggests checking the ProjectTypeGuids in his blog post about Manually creating a MS Test Project. Apparently the magic GUIDs you need are {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} for c# and {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{F184B08F-C81C-45F6-A57F-5ABD9991F28F} for VB. See his blog post for more details.
In case the blog post ever goes away you need to add the following element in the main property group in the csproj file:
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
Another idea for the Googlers out there. My problem was trying to get ignored tests running again. Same MS error message occurs if you remove the Ignore label. Does not automatically re-enable the test. This article takes you through the last step. http://richallen.blogspot.com/2008/05/ms-test-re-enabling-ignored-tests.html
The fix is simple, even though it shouldn't be needed, if Visual Studio worked as it should.
To sum up what others have contributed, particularly in this article, here's what ultimately worked for me:
Use the Configuration Manager to make sure your test project is selected to build in whatever configuration and platform you're using (ex: configuration=Debug and platform=x86)
Make sure your method belongs to a [TestClass] and that it's both marked [TestMethod], and NOT using the attribute [Ignore]
Use Test View to find your test.
Open your Properties window (F4), and make sure your test is enabled
The original poster did do this, but I arrived here after not having done this:
Be sure that [TestClass] is declared at the top, public in scope:
namespace XYZ.API.Repository.Tests
{
[TestClass()]
public class ClientTests
{
I was receiving the same message and it turned out to be that I had my unit test project on a network drive. Once I moved it local it ran fine. Just something to try if you get this error.
John
I've just manually done this:
Created a new C# class library project with the following code:
namespace SO_Answer
{
public class Class1
{
public void Test()
{
var k = "Hello";
}
}
}
Saved the project and then went to 'File->Add->New Project' and chose 'Test Project'. After VS created the unit test project, I added a reference to the class library project I created earlier.
In my test I have this code:
namespace Unit_Test
{
/// <summary>
/// Summary description for UnitTest1
/// </summary>
[TestClass]
public class UnitTest1
{
/// <summary>
///Gets or sets the test context which provides
///information about and functionality for the current test run.
///</summary>
public TestContext TestContext { get; set; }
#region Additional test attributes
// You can use the following additional attributes as you write your tests:
// Use ClassInitialize to run code before running the first test in the class
// [ClassInitialize()]
// public static void MyClassInitialize(TestContext testContext) { }
// Use ClassCleanup to run code after all tests in a class have run
// [ClassCleanup()]
// public static void MyClassCleanup() { }
// Use TestInitialize to run code before running each test
// [TestInitialize()]
// public void MyTestInitialize() { }
// Use TestCleanup to run code after each test has run
// [TestCleanup()]
// public void MyTestCleanup() { }
#endregion
/// <summary>
/// The test method 1.
/// </summary>
[TestMethod]
public void TestMethod1()
{
var f = new Class1();
}
}
}
The only code I added was the a using statement and the var f = new Class1(); statement. Looking at the MSTest runner, I can see TestMethod1 appear.
I can't think of a reason why your unit tests are not being picked up. The only time I've had this is because I was using the MSTest runner to try and view NUnit tests by mistake. Try starting from scratch.
This could be another reason. Check whether the solution is running on 64bit. If so change it to x86.
This must be a bug and is an absolute pain especially as you have to reenable every single test method individually. However a bit iof lateral thinking produced a better solution - rename the test class and rebuild. Then rename it back. Seems to work.
Ooops - no it doesn't. Renaming the class works but when it's renamed back it reverts to the original settings.
The trick is to close down Visual Studio and delete the .vsmdi (visual studio test meta data) file. This will be regenrated.
When you run into this issue, in Visual Studio, you have to create a Test Project.
1. Select Test in Tool bar and choose "New Test". Create your project and at this point create your test method. It should work after this point.
For posterity: I just found that marking tests as static made them silently fail to appear in the test list. Apparently that isn't allowed.
None of the other answers worked for me. I kept getting the following message in the output window:
------ Discover test started ------
========== Discover test finished: 2 found (0:00:00.1310428) ==========
No tests found to run.
In my case, the problem only occurred after I created a new configuration called 0-Local. I had to add <DebugSymbols>true</DebugSymbols to the relevant section of my csproj file, so it looks like this:
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == '0-Local|AnyCPU'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\0-Local\</OutputPath>
</PropertyGroup>
Do you have a VSMDI file in your solution? I believe this file is required (NOT VERIFIED).
this is typical problem i have faced too. but the easiest solution I followed as my own is...just to build the project once and rebuild it again. so that you can resolve it.
Had this same issue but reading over the previous answers, everything looked good.
In my case, I had just run the test suite made a small change, built the solution and tried to run the test. No go. I tried building a couple more times and looking for problems other people had tried. Still no go.
I hit enter in one of my test methods to add a new and hit F6 to build the solution and clicked run Unit Tests.
Bingo! Everything ran smoothly.
I was making use of a public TestContext TestContext method to write to the test output and changed the scope to private. This made every test not discoverable. Changing it back to public helped.
Another one for googlers using NUnit, especially those who have migrated from MS Unit test to NUnit. Please remove the project type Guids that are identifying the project as MS Test project from the project file.
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
If your code is CLI (managed c++) and your test class inherits from an abstract base class, make sure that your test class implements the base's pure virtual method.
if you didn't implement it, you may see the "no tests found to run" message.