471,353 Members | 1,711 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,353 software developers and data experts.

Testing classes individually

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.

Dec 22 '05 #1
6 1535
In that case, you would have to make a single project file for each class.

"Huy Hoang" <dr*****@gmail.com> schrieb im Newsbeitrag
news:11**********************@g14g2000cwa.googlegr oups.com...
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.

Dec 22 '05 #2

"Huy Hoang" <dr*****@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
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?
Dec 22 '05 #3
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".

Dec 22 '05 #4
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

*** Sent via Developersdex http://www.developersdex.com ***
Dec 22 '05 #5
Huy Hoang <dr*****@gmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 22 '05 #6
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.

Dec 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Hugh Cowan | last post: by
17 posts views Thread by Phil Powell | last post: by
3 posts views Thread by Andrew | last post: by
3 posts views Thread by SnaiL | last post: by
58 posts views Thread by nw | last post: by
20 posts views Thread by earthwormgaz | last post: by
reply views Thread by Matthew Fitzgibbons | last post: by
10 posts views Thread by Brendan Miller | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.