Testing classes individually

  • Thread starter Thread starter Huy Hoang
  • Start date Start date
H

Huy Hoang

I remember that in Java you can compile and run each class individually
as long as it contains the method "main". However, is this possible in
C# as well? I tried it a while ago but it didn't work.
 
Huy Hoang said:
I remember that in Java you can compile and run each class individually
as long as it contains the method "main". However, is this possible in
C# as well? I tried it a while ago but it didn't work.

I just wrote a simple class, then added a Main() method with the header:

static void Main(string[] args)

I then instantiated my class and added some test code in the Main() method.

Worked fine.

How did you try to do it?
 
Let's say I have a VS solution with 10 VC# applications, and this
Visual C# Class Library project with the following default files:

- AssemblyInfo.cs
- Class1.cs

I then add the Class Library project as a reference to all the other
projects. The other projects can instantiate the Class1.cs just fine.
However, to test the class "Class1" individually without writing test
projects, but just like Java with a main method, seems impossible in
Visual Studio .NET 2003.

In JBuilder I am able to right click a class, and build/run it (if it
contains the "main" method). But in Visual Studio there is only this
"(Re)build [Projectname]" and "(Re)build solution".
 
Huy..

5) The Visual Studio IDE Startup Object is Empty By Default
If the startup object string is empty and you declare more than one
"Main" method in a Visual Studio project, the project will not compile.
This is very frustrating. Trying to set the startup object property in
the Visual Studio IDE is non-trivial. Let me save you some pain. To set
the startup object, select View --> Solution Explorer. In the Solution
Explorer window select the name of the project. Now select View -->
Property Pages. (Do not select the Property Window!) Alternatively,
right click on the project name and select Properties. Now select the
Common Properties folder in the left window. You can now select the
appropriate startup object from the drop down list. Don't forget to
click on "Apply."

http://www.geocities.com/Jeff_Louie/OOP/oop5.htm

In VS2005 you can now just right click.

Regards,
Jeff
 
Huy Hoang said:
I remember that in Java you can compile and run each class individually
as long as it contains the method "main". However, is this possible in
C# as well? I tried it a while ago but it didn't work.

You can specify the startup object in a .NET project, but this is *not*
the best way of testing classes.

Instead, get hold of a unit testing framework (eg NUnit -
http://www.nunit.org) and use that. It makes it much easier to build
whole suites of tests and run them at will, with immediate feedback
about whether they've worked or not.
 
Jeff,

Setting the startup object isn't that hard, indeed. It's just that this
way of working is very frustrating.
That's why I will avoid it.
 
Back
Top