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

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.

Related

How to unit test a project outside visual studio

I have made a project in visual studio and also implemented unit testing.
The code working fine with the unit testing.
Now I have a doubt that is it possible to implement unit testing outside the visual studio environment such that I only use the exe generated by my project and test it for multiple cases?
I am looking for an option which can utilise my current unit testing implementation
I am new to unit testing, so any help would be appreciated
thanks in advance
Installing MSTest without Visual Studio is not quite trivial thing. This tool was very useful to me in this regard.
Originated from here
You can use outside library like NUnit, there is manager to run your test
If from outside, you mean outside of Visual Studio IDE, then you can use mstest.exe which VS internally uses. This is stand alone in sense, that host doesn't need VS installed. Thus e.g. a build system can call this and do unit test towards the end of build.
If you are using the Microsoft test framework, you can run MSTest.exe from the command line.
If you set up your test classes as per this SO answer then you can run them in either MSTest or NUnit, based on a compilation option. Hope this helps!
Using both MSTest and NUnit?

C# .NET 4.0 Testing Framework?

If I'm not mistaken, NUnit is the de-facto standard for unit testing, but I've just downloaded it, wrote a simple test, and then apparently I have to fire up the GUI and load my .exe assembly, which simply failed.
I tried editing
C:\Program Files (x86)\NUnit 2.5.7\bin\net-2.0\nunit.exe.config
As suggested in this question, but that didn't work either, so I tried downloading the nunit source code and compiling it in vs2010, but it doesn't even compile. Says punit.framework.dll could not be found. That solution says "does not contain a definition for AllTestsExecuted", so I'm getting a little frustrated here. You'd think there would be an easy-to-use-and-get-running framework for .net 4, no?
So my question is, how do I either get NUnit working, or is there another framework that will cause me less agony?
You don't have to use the NUnit GUI to run your tests. You can use TestDriven.NET from within Visual Studio. Also, if you happen to be using Resharper, that has a unit test runner which works with NUnit also.
If you're not doing anything out of the ordinary, I recommend Microsoft's Unit Testing Framework. I find it's VS integration too easy to even worry about NUnit. I agree NUnit seems to be the defacto standard, but if you're looking for something quick and easy. Microsoft's way is the easiest for a typical Visual Studio programmer IMHO.
I am not a C# programmer (fortunately ;-) ) but I've heard good things about xUnit. Tests can be run pretty much however you want (command line, GUI, Visual Studio integration, and more) and it looks reasonable simple to use.
For NUnit's GUI test runner, make sure you've selected the right framework version. Its in the "File" menu. If your test or any dependencies are 32-bit be sure you're running the 32bit version of the test runner.
Testdriven.net is a better test runner, but I like using NUnit's GUI runner too at times.
NUnit is infact very simple to use, so I would say that it's more likely that you are making a mistake somewhere, not the software.
Make sure you follow this guide.
Ensure that you have the [TestFixture] and [Test] attributes in the correct places and all the relevant assemblies referenced.
Make sure that you are loading the correct dll in the NUnit GUI.
If its the GUI that is the issue, you can use Resharper's unit testing feature in stead.
If you use Visual Studio 2010, you can use MSTest. Just click CTRL + ALT + R and it will run your tests and show the results in Visual Studio itself.
That same test-runner will also work for NUnit, if I am not mistaken.

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.

Automating GUI testing using C#

I am doing on a project to built automatic GUI testing for graphical application in .NET. I will use C# but i am trying to reading to get some ideas. But I don't have any idea on how to record and replay back. So can you give me your ideas.
Here's a blog from the visual studio team that goes over this exact topic. It's about how they tested pre-wpf and post-wpf.
This post covers an overview of techniques that we used to create and maintain automated user interface regression tests for Visual Studio. Regression tests are a type of software test that, collectively, aim to be an oracle of expected functionality for the target application, run often against new builds of product – they aim to uncover regressions in behavior introduced in a new build.
Visual Studio Blog
Selenium RC!
There is also the TFS Test that has web tests, which tie into load test. Nice platform, but if you are not already using TFS then it is not worth it.
Frankly, I don't think there is a good answer out there for this. There are two options, however, that work okay:
Visual Studio Test Edition (as durilai suggested)
Telerik's Web Testing (both a free version and a version with a cost but depending on your UI, this may or may not work)
A good (commercial) C#-based product is Ranorex, it may do what you need.
Have you updated your Visual Studio to 2010 version? It has a build-in coded-ui automated testing feature, which could be used to record and replay back the test for win form.
Selenium
Watin
are some options that come to mind
To test C# apps there are a few things we have had success with:
PowerShell
TestPlant
and possibly would be the new VSTS 2010 features, though we haven't tried them

How to debug a class library in Visual Studio

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.

Categories