How to debug a class library in Visual Studio - c#

I am working on a class library (DLL) project in Visual Studio 2008; programming in C#. In order to test my DLL I just created a second project that is a console application and in that project I can reference the first and run tests. Is there a simpler way of doing this? Can I just create another file within my class library project that has the tests in it and then somehow tell Visual Studio to run that file?
I know one way would be to add a text file to my project and then write my test code in JScript. Then in the Project settings on the debug menu I can tell it to Start External Program (JScript). Then, the name of my test file, test.js, goes in the Command Line Arguments box. But, I am wondering if there is a way to do it using C# code instead of JScript?

You could add a testing project to your current solution, then set that project as the startup project. Then, hitting F5 on your class library project will start your testing project.

Take a look at NUnit or other similar unit testing framework.
The "Team Developer" and "Team Suite" flavors of Visual Studio already have Microsoft's unit testing framework built in.

Create a unit test project for the class library by using the right-click "Create Unit Tests" in a class/method in the library. I would recommend downloading TestDriven.NET and using the right-click test runner in it.

Are you talking about unit tests? You can use something like nUnit or the built in testing framework that comes with Visual Studio. The simplest tests just require you to add some attributes to your test fixture and make an assertion like obj1 == obj2.
Checking out something like Test-Driven Development (TDD), Domain-Driven Development (DDD) or Behavioral-Driven Development (BDD) may be beneficial. I like to use nUnit with nBehave, myself.

Related

ASP.NET MVC unit testing

I have created a program in Visual Studio 2017, and I just noticed now that I forgot to add unit testing when creating it, and I am too far gone to start over.
How can I solve this problem?
You should be able to right-click on the solution, and Add Project, then Unit Test Project. Unless you're not using MSTest, then add a new project and choose class library.

why my test project doesn't appear on test explorer

I am using VS2012 (v110)
I create a simple dll application with only 1 function and a test project to test the dll function. I set up both projects to be debug builds for win32 and tried Clean and Rebuild both projects but I don't see any test to be run on the test Explorer windows.
I also reference Dll project in the test project already.
Why isn't there anything shown in Test Explorer windows?
Actually MSTest doesn't work with simple class library projects.
You will have to create a unit test project and place your tests there.
If you use other testing frameworks like NUnit, then you can use plain class library. You can then use NUnit to run the tests outside VS, or use tools like ReSharper to run those tests inside VS
It seems you have used some test frameworks other that MSTest. If you want to use Test Explorer for some other frameworks you should install its runner too. for instance NUnit needs to install its test adapter as and extension in VS.NET
see here for more.
You should be able to modify an existing class library project to get mstest to recognize it as a test project by modifying the ProjectTypeGuids in the .csproj file as detailed in this answer How to get VS2010 to recognize my mstests generated by SpecFlow? (Look for the answer that lists the specific guids and not the currently accepted answer which says to just create a new project.)
Have had a simular problem. Solved it. I have been adding tests project to my VS 2012 solution and tests were not appearing in Test Explorer. Problem was that test project was located in IIS inetpub\wwwroot folder which didn't have windows user rights to modify it. Adding solution folder Windows user rights to modify it solved issue. Tests are now visible in test explorer and able to be debugged. I assume that vstest.discoveryengine.exe process with is run by windows user was not having access to test project files

Turn visual studio project testfixtures into nunit dll

I have a visual studio project. I later added a .cs file for testing and have added the appropriate 'using' statements and [Test] and [Testfixture] attributes. When I open the nunit gui application, however, it only accepts .dll, .exe, and .nunit files. I was wondering what exact steps I need to take to execute the tests I wrote. Is it possible to do so directly in visual studio?
You should add the NUnit dll as a reference on your project.
JetBrains' Resharper is able to run the tests inside Visual Studio. Read more about Resharpers unittesting here. JetBrains has also developed a line coverage tool: dotCover.
Right now, Resharper is the best solution.
Test Driven .Net is the best free solution I know of.
Visual Studio 11 will finally allow for 3rd part testing framework plugins.

C# testing framework that works like JUnit in Eclipse?

I come from a Java/Eclipse background and I fear that I am spoiled by how easy it is to get JUnit and JMock running in Eclipse, and have that GUI with the bar and pass/fail information pop up. It just works with no hassle.
I see a lot of great options for testing in C# with Visual Studio. NUnit looks really nice because it contains unit and mock testing all in one. The trouble is, I can't figure out how to get the IDE display my results. The NUnit documentation seems to show that it doesn't automatically show results through the VS IDE. I found http://testdriven.net/, which seems to trumpet that is makes VS display these stats and work with multiple frameworks, but it isn't open source.
Is there anyway to get unit and mock testing working with the VS IDE like it does in Java with Eclipse?
On installing NUnit you get an NUnit.exe - use this to open and run your tests. It has an UI and shows pass/fails and shows output.
You can add a build action in Visual Studio that on a specific testing configuration will build, then immediately invoke NUnit on that dll.
EDIT: (more details)
In test project:
Project Properties -> Debug (set a build configuration - I use "NUnitDebug")
Start Action -> "Start external program": C:\Program Files\NUnit 2.5.3\bin\net-2.0\nunit.exe (use your own path)
Start Options -> Command line arguments: MyTestProject.dll (replace with the name of your DLL)
EDIT2: As brendan said, Moq is a good mock framework that can be used.
Resharper will let you do this and has a nice UI. I believe the core of it is NUnit. For the mock stuff you'll want to use Moq.
Resharper is not free/open source but is so worth the price.
If you are looking for something like Eclipse/JUnit, you shouldn't have tried Microsoft product line.
But the good news is that SharpDevelop has such nice integration with NUnit and it is open source. However, it aims as an alternative to VS, not an addon for VS.
You could read ASP.NET MVC Test Framework Integration Walkthrough and run your tests from the VS test runner.
Have you tried using the Testing projects in Visual studio? They're practically identical to nUnit, and can be run simply by hitting F5.
For mocking, chose whichever suits you, We're looking at Moq for Silverlight support.

How to run a C# main in an ASP.NET app

I'm developing an ASP.NET 2.0 app using Visual Studio 2008.
If I want to run a really quick test on a method that's way in my back-end, is there a way for me to just call a main function in that class via command line?
Thanks
Short answer: NUnit. You may not know how to use it, but you should. It's not hard to use and learn. It's fast and has a GUI.
That's what a test project is made for.
You should get TestDriven.NET add-in (free for personal use). Basically, it's a bundle of Test Driven Development tools such as NUnit, which integrates with your Visual Studio. One thing I discovered about it, is that it allows you to run any method in your code, just by right-clicking on it and choosing the menu item "Run Test(s)", or "Test With -> Debugger" if you want to debug the method.
Hope that helps.
The answer is no, You cannot do that. You can only have one main function per assembly.
The fact is, you shouldn't do testing like that. C# is not Java, regardless of its origin in Java.
Use NUnit or MSUnit and build unit tests instead. They'll test your methods for you without needing deployment to a website or anything like that. That's the best way to test a method. Here are some links:
NUnit
MSUnit
Simply create a test project and test it from there. If not you can create a console application and test it from there by referencing the proper project(considering your code to test is in an assembly), which in a way will be your test project.

Categories