473,735 Members | 3,948 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# - Attributes - Unit Tests Question

Hi

I want to place the tests needed in the code using attributes. There seems
to be enough code snippets around
for me to cover this. e.g.

// Test cases, run these here on the function and check the result

[Test, Function=CheckT ype, Args="-1,3,'flower'", Result=true]

[Test, Function=CheckT ype, Args="5,3,'pot' ", Result=true]

[Test, Function=CheckT ype, Args="99,3,'men '", Result=false]

// Function

static Boolean CheckType(int FirstArg, int SecondArg, string sType)

{

// Code in here

...

return bResult;

}

The harder part is i want to use reflection to establish the function
arguments and create on the

stack these arguments with the correct values and then call the function
trapping the returned value .

This would be fantastic as nolonger would 2 sets of code need to be
maintained, i.e. the original

functions then the test cases which exercised these functions.

The test for the function lives with the function .
Can you comment if this is possible and if there is an example great ?

C# begineer

Thanks in advance
Nov 17 '05 #1
16 2569
Hi Greg,
what testing framework are you using for your unit tests, or are you
trying to write your own?

You do not want the test for your function to live with your production
code, as this adds extra code size, and makes the code more difficult to read
- plus you are sending test code to your end customers - you should seperate
your test code from the code it is testing. ie have all your testing code
live in a different project from your production code.

If you have not heard of NUnit (www.nunit.org) then it would be really good
to check this out, it is simple but really effective and takes the hassle out
of you having to write your own testing framework.

You should try to write one test function per action you want to test, this
way you do not need to have parameters passed to your test function, and
inside the function you initialize the object you want to test and pass in
the values to the object.

Take for example some ficticious object called MyAdditionObjec t which is
used to add two numbers, to test it (using NUnit) you would write something
like:

using NUnit.Framework ;

[TestFixture]
public class MyAdditionObjec tTestFixture
{
[Test]
public void TestAddNumbers( )
{
MyAdditionObjec t myAddObj = new MyAdditionObjec t();

int intResult = myAddObj.AddTwo Numbers(2,3);

//check the result is correct, this is an object native to NUnit
Assert.AreEqual (5, intResult);
}

[Test]
public void TestAddNegative PositiveNumber( )
{
MyAdditionObjec t myAddObj = new MyAdditionObjec t();

int intResult = myAddObj.AddTwo Numbers(-2,3);

//check the result is correct, this is an object native to NUnit
Assert.AreEqual (1, intResult);
}
}

You can see your parameters live inside the testing functions and all test
functions are parameterless.

Hope this helps
Mark R Dawson
"Greg Roberts" wrote:
Hi

I want to place the tests needed in the code using attributes. There seems
to be enough code snippets around
for me to cover this. e.g.

// Test cases, run these here on the function and check the result

[Test, Function=CheckT ype, Args="-1,3,'flower'", Result=true]

[Test, Function=CheckT ype, Args="5,3,'pot' ", Result=true]

[Test, Function=CheckT ype, Args="99,3,'men '", Result=false]

// Function

static Boolean CheckType(int FirstArg, int SecondArg, string sType)

{

// Code in here

...

return bResult;

}

The harder part is i want to use reflection to establish the function
arguments and create on the

stack these arguments with the correct values and then call the function
trapping the returned value .

This would be fantastic as nolonger would 2 sets of code need to be
maintained, i.e. the original

functions then the test cases which exercised these functions.

The test for the function lives with the function .
Can you comment if this is possible and if there is an example great ?

C# begineer

Thanks in advance

Nov 17 '05 #2
Thanks for the feedback, some more info

Yes we are using MSTest, NUnit and a few others in various old / new C++
(non managed) / C# products .

We are starting a new C# (mainly) feature set and i am researching what is
possible to make for the best
test setup. I am relatively new to C#. Done the few odd utilities.

Currently if you add or change a function, you then need to go to the test
framemowork and add / adjust
, and in some cases due to a switch / case in the test which also nees to
have new numbers defined.

To me with the ability to hide text in C# having most of the "test cases"
living with the actual code would be ideal.

Hence this post.

Thanks

"Mark R. Dawson" <Ma*********@di scussions.micro soft.com> wrote in message
news:AF******** *************** ***********@mic rosoft.com...
Hi Greg,
what testing framework are you using for your unit tests, or are you
trying to write your own?

You do not want the test for your function to live with your production
code, as this adds extra code size, and makes the code more difficult to
read
- plus you are sending test code to your end customers - you should
seperate
your test code from the code it is testing. ie have all your testing code
live in a different project from your production code.

If you have not heard of NUnit (www.nunit.org) then it would be really
good
to check this out, it is simple but really effective and takes the hassle
out
of you having to write your own testing framework.

You should try to write one test function per action you want to test,
this
way you do not need to have parameters passed to your test function, and
inside the function you initialize the object you want to test and pass in
the values to the object.

Take for example some ficticious object called MyAdditionObjec t which is
used to add two numbers, to test it (using NUnit) you would write
something
like:

using NUnit.Framework ;

[TestFixture]
public class MyAdditionObjec tTestFixture
{
[Test]
public void TestAddNumbers( )
{
MyAdditionObjec t myAddObj = new MyAdditionObjec t();

int intResult = myAddObj.AddTwo Numbers(2,3);

//check the result is correct, this is an object native to NUnit
Assert.AreEqual (5, intResult);
}

[Test]
public void TestAddNegative PositiveNumber( )
{
MyAdditionObjec t myAddObj = new MyAdditionObjec t();

int intResult = myAddObj.AddTwo Numbers(-2,3);

//check the result is correct, this is an object native to NUnit
Assert.AreEqual (1, intResult);
}
}

You can see your parameters live inside the testing functions and all test
functions are parameterless.

Hope this helps
Mark R Dawson
"Greg Roberts" wrote:
Hi

I want to place the tests needed in the code using attributes. There
seems
to be enough code snippets around
for me to cover this. e.g.

// Test cases, run these here on the function and check the result

[Test, Function=CheckT ype, Args="-1,3,'flower'", Result=true]

[Test, Function=CheckT ype, Args="5,3,'pot' ", Result=true]

[Test, Function=CheckT ype, Args="99,3,'men '", Result=false]

// Function

static Boolean CheckType(int FirstArg, int SecondArg, string sType)

{

// Code in here

...

return bResult;

}

The harder part is i want to use reflection to establish the function
arguments and create on the

stack these arguments with the correct values and then call the function
trapping the returned value .

This would be fantastic as nolonger would 2 sets of code need to be
maintained, i.e. the original

functions then the test cases which exercised these functions.

The test for the function lives with the function .
>> Can you comment if this is possible and if there is an example great ?

C# begineer

Thanks in advance

Nov 17 '05 #3
Hi Greg,
what testing framework are you using for your unit tests, or are you
trying to write your own?

You do not want the test for your function to live with your production
code, as this adds extra code size, and makes the code more difficult to read
- plus you are sending test code to your end customers - you should seperate
your test code from the code it is testing. ie have all your testing code
live in a different project from your production code.

If you have not heard of NUnit (www.nunit.org) then it would be really good
to check this out, it is simple but really effective and takes the hassle out
of you having to write your own testing framework.

You should try to write one test function per action you want to test, this
way you do not need to have parameters passed to your test function, and
inside the function you initialize the object you want to test and pass in
the values to the object.

Take for example some ficticious object called MyAdditionObjec t which is
used to add two numbers, to test it (using NUnit) you would write something
like:

using NUnit.Framework ;

[TestFixture]
public class MyAdditionObjec tTestFixture
{
[Test]
public void TestAddNumbers( )
{
MyAdditionObjec t myAddObj = new MyAdditionObjec t();

int intResult = myAddObj.AddTwo Numbers(2,3);

//check the result is correct, this is an object native to NUnit
Assert.AreEqual (5, intResult);
}

[Test]
public void TestAddNegative PositiveNumber( )
{
MyAdditionObjec t myAddObj = new MyAdditionObjec t();

int intResult = myAddObj.AddTwo Numbers(-2,3);

//check the result is correct, this is an object native to NUnit
Assert.AreEqual (1, intResult);
}
}

You can see your parameters live inside the testing functions and all test
functions are parameterless.

Hope this helps
Mark R Dawson
"Greg Roberts" wrote:
Hi

I want to place the tests needed in the code using attributes. There seems
to be enough code snippets around
for me to cover this. e.g.

// Test cases, run these here on the function and check the result

[Test, Function=CheckT ype, Args="-1,3,'flower'", Result=true]

[Test, Function=CheckT ype, Args="5,3,'pot' ", Result=true]

[Test, Function=CheckT ype, Args="99,3,'men '", Result=false]

// Function

static Boolean CheckType(int FirstArg, int SecondArg, string sType)

{

// Code in here

...

return bResult;

}

The harder part is i want to use reflection to establish the function
arguments and create on the

stack these arguments with the correct values and then call the function
trapping the returned value .

This would be fantastic as nolonger would 2 sets of code need to be
maintained, i.e. the original

functions then the test cases which exercised these functions.

The test for the function lives with the function .
Can you comment if this is possible and if there is an example great ?

C# begineer

Thanks in advance

Nov 17 '05 #4
Thanks for the feedback, some more info

Yes we are using MSTest, NUnit and a few others in various old / new C++
(non managed) / C# products .

We are starting a new C# (mainly) feature set and i am researching what is
possible to make for the best
test setup. I am relatively new to C#. Done the few odd utilities.

Currently if you add or change a function, you then need to go to the test
framemowork and add / adjust
, and in some cases due to a switch / case in the test which also nees to
have new numbers defined.

To me with the ability to hide text in C# having most of the "test cases"
living with the actual code would be ideal.

Hence this post.

Thanks

"Mark R. Dawson" <Ma*********@di scussions.micro soft.com> wrote in message
news:AF******** *************** ***********@mic rosoft.com...
Hi Greg,
what testing framework are you using for your unit tests, or are you
trying to write your own?

You do not want the test for your function to live with your production
code, as this adds extra code size, and makes the code more difficult to
read
- plus you are sending test code to your end customers - you should
seperate
your test code from the code it is testing. ie have all your testing code
live in a different project from your production code.

If you have not heard of NUnit (www.nunit.org) then it would be really
good
to check this out, it is simple but really effective and takes the hassle
out
of you having to write your own testing framework.

You should try to write one test function per action you want to test,
this
way you do not need to have parameters passed to your test function, and
inside the function you initialize the object you want to test and pass in
the values to the object.

Take for example some ficticious object called MyAdditionObjec t which is
used to add two numbers, to test it (using NUnit) you would write
something
like:

using NUnit.Framework ;

[TestFixture]
public class MyAdditionObjec tTestFixture
{
[Test]
public void TestAddNumbers( )
{
MyAdditionObjec t myAddObj = new MyAdditionObjec t();

int intResult = myAddObj.AddTwo Numbers(2,3);

//check the result is correct, this is an object native to NUnit
Assert.AreEqual (5, intResult);
}

[Test]
public void TestAddNegative PositiveNumber( )
{
MyAdditionObjec t myAddObj = new MyAdditionObjec t();

int intResult = myAddObj.AddTwo Numbers(-2,3);

//check the result is correct, this is an object native to NUnit
Assert.AreEqual (1, intResult);
}
}

You can see your parameters live inside the testing functions and all test
functions are parameterless.

Hope this helps
Mark R Dawson
"Greg Roberts" wrote:
Hi

I want to place the tests needed in the code using attributes. There
seems
to be enough code snippets around
for me to cover this. e.g.

// Test cases, run these here on the function and check the result

[Test, Function=CheckT ype, Args="-1,3,'flower'", Result=true]

[Test, Function=CheckT ype, Args="5,3,'pot' ", Result=true]

[Test, Function=CheckT ype, Args="99,3,'men '", Result=false]

// Function

static Boolean CheckType(int FirstArg, int SecondArg, string sType)

{

// Code in here

...

return bResult;

}

The harder part is i want to use reflection to establish the function
arguments and create on the

stack these arguments with the correct values and then call the function
trapping the returned value .

This would be fantastic as nolonger would 2 sets of code need to be
maintained, i.e. the original

functions then the test cases which exercised these functions.

The test for the function lives with the function .
>> Can you comment if this is possible and if there is an example great ?

C# begineer

Thanks in advance

Nov 17 '05 #5


Greg Roberts wrote:
Hi
// Test cases, run these here on the function and check the result

[Test, Function=CheckT ype, Args="-1,3,'flower'", Result=true]

[Test, Function=CheckT ype, Args="5,3,'pot' ", Result=true]

[Test, Function=CheckT ype, Args="99,3,'men '", Result=false] .... static Boolean CheckType(int FirstArg, int SecondArg, string sType)
{
return bResult;
}
This seems like a very complicated way of doing:

// Here comes the test framework
public class CheckException: Exception { }
void CHECK(boolean b) { if (!b) throw new CheckException( ); }
// here comes the test
void testFoo() {
CHECK(CheckType (-1,3,"flower") == true);
CHECK(CheckType (5,3,"pot") == true);
CHECK(CheckType (99,3,"men") == false);
}
The harder part is i want to use reflection to establish the function
arguments and create on the
stack these arguments with the correct values and then call the function
trapping the returned value .

This would be fantastic as nolonger would 2 sets of code need to be
maintained, i.e. the original

functions then the test cases which exercised these functions.

The test for the function lives with the function .


You just formulated a new language ([Test, ...]), which is certainly not
as rich as C#. I try to formulate problems in the source language
instead of implementing my own.
Can you comment if this is possible and if there is an example great ?


nUnit does stuff like this. But I have a hard time seeing the point in
these frameworks.

--
Helge Jensen
mailto:he****** ****@slog.dk
sip:he********* *@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Nov 17 '05 #6


Greg Roberts wrote:
Hi
// Test cases, run these here on the function and check the result

[Test, Function=CheckT ype, Args="-1,3,'flower'", Result=true]

[Test, Function=CheckT ype, Args="5,3,'pot' ", Result=true]

[Test, Function=CheckT ype, Args="99,3,'men '", Result=false] .... static Boolean CheckType(int FirstArg, int SecondArg, string sType)
{
return bResult;
}
This seems like a very complicated way of doing:

// Here comes the test framework
public class CheckException: Exception { }
void CHECK(boolean b) { if (!b) throw new CheckException( ); }
// here comes the test
void testFoo() {
CHECK(CheckType (-1,3,"flower") == true);
CHECK(CheckType (5,3,"pot") == true);
CHECK(CheckType (99,3,"men") == false);
}
The harder part is i want to use reflection to establish the function
arguments and create on the
stack these arguments with the correct values and then call the function
trapping the returned value .

This would be fantastic as nolonger would 2 sets of code need to be
maintained, i.e. the original

functions then the test cases which exercised these functions.

The test for the function lives with the function .


You just formulated a new language ([Test, ...]), which is certainly not
as rich as C#. I try to formulate problems in the source language
instead of implementing my own.
Can you comment if this is possible and if there is an example great ?


nUnit does stuff like this. But I have a hard time seeing the point in
these frameworks.

--
Helge Jensen
mailto:he****** ****@slog.dk
sip:he********* *@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Nov 17 '05 #7
Greg Roberts <gr*********@NO SPAMLOWLIFEcite ct.com> wrote:
Thanks for the feedback, some more info

Yes we are using MSTest, NUnit and a few others in various old / new C++
(non managed) / C# products .

We are starting a new C# (mainly) feature set and i am researching what is
possible to make for the best
test setup. I am relatively new to C#. Done the few odd utilities.

Currently if you add or change a function, you then need to go to the
test framemowork and add / adjust , and in some cases due to a switch
/ case in the test which also nees to have new numbers defined.
But that's exactly the right way of working, in my view - separate test
code from the implementation code, and write the tests without looking
at the implementation. That way you're encouraged to think of cases
which the code might not cover - otherwise you often end up just
looking at the "if" conditions and writing a different test for each of
them, which may miss some conditions that you *should* be
distinguishing in the code, but aren't.

I think it's good to have the test code in the same solution, but in a
different project.
To me with the ability to hide text in C# having most of the "test cases"
living with the actual code would be ideal.


So you're happy with your tests ending up being part of your final app?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #8
Greg Roberts <gr*********@NO SPAMLOWLIFEcite ct.com> wrote:
Thanks for the feedback, some more info

Yes we are using MSTest, NUnit and a few others in various old / new C++
(non managed) / C# products .

We are starting a new C# (mainly) feature set and i am researching what is
possible to make for the best
test setup. I am relatively new to C#. Done the few odd utilities.

Currently if you add or change a function, you then need to go to the
test framemowork and add / adjust , and in some cases due to a switch
/ case in the test which also nees to have new numbers defined.
But that's exactly the right way of working, in my view - separate test
code from the implementation code, and write the tests without looking
at the implementation. That way you're encouraged to think of cases
which the code might not cover - otherwise you often end up just
looking at the "if" conditions and writing a different test for each of
them, which may miss some conditions that you *should* be
distinguishing in the code, but aren't.

I think it's good to have the test code in the same solution, but in a
different project.
To me with the ability to hide text in C# having most of the "test cases"
living with the actual code would be ideal.


So you're happy with your tests ending up being part of your final app?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #9
Helge Jensen <he**********@s log.dk> wrote:
Can you comment if this is possible and if there is an example great ?

nUnit does stuff like this.


I haven't seen anything in NUnit for that - could you point me at a
reference?
But I have a hard time seeing the point in these frameworks.


Do you mean you can't see the point in unit testing frameworks in
general, or just this kind of feature?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

38
3406
by: Christoph Zwerschke | last post by:
In August 2001, there was a thread about the "Art of Unit Testing": http://groups.google.com/group/comp.lang.python/browse_frm/thread/aa2bd17e7f995d05/71a29faf0a0485d5 Paul Moore asked the legitimate question why there is no hook for a "global" fixture code that is run only once for the whole TestCase, as opposed to the normal "setUp" and "tearDown" code that is run for every single test in the TestCase. A "global fixture" would be...
14
2750
by: | last post by:
Hi! I'm looking for unit-testing tools for .NET. Somthing like Java has --> http://www.junit.org regards, gicio
2
2107
by: Naveen Mukkelli | last post by:
Hi, Currently, I've got all my unit tests in a separate class in the same project. Instead of this, I'm planning to move all my tests in a separate project in a single solution. For example let "TargetProject" is the main project in a solution. My plan is to have another project, for example "TestTargetProject" that will
5
3942
by: David K Allen | last post by:
Here's my problem. Have a database interface with several methods. I created a database class to implement the interface, and a set of unit tests to test each method. Now I want to support another database provider. The implementation is very easy: I simply create a new database class, and implement all the methods using the new database provider. My question is: How do I get this same set of unit tests to run in a loop? My problem is...
72
5264
by: Jacob | last post by:
I have compiled a set og unit testing recommendations based on my own experience on the concept. Feedback and suggestions for improvements are appreciated: http://geosoft.no/development/unittesting.html Thanks.
28
1472
by: Alan Isaac | last post by:
I have a class whose instances should only receive attribute assignments for attributes that were created at inititialization. If slots are not appropriate, what is the Pythonic design for this? Thanks, Alan Isaac
176
8401
by: nw | last post by:
Hi, I previously asked for suggestions on teaching testing in C++. Based on some of the replies I received I decided that best way to proceed would be to teach the students how they might write their own unit test framework, and then in a lab session see if I can get them to write their own. To give them an example I've created the following UTF class (with a simple test program following). I would welcome and suggestions on how anybody...
5
2842
by: =?Utf-8?B?cmFuZHkxMjAw?= | last post by:
I'm working in Visual Studio 2005 Team Edition for Software Developers I've used the wizard under the Test menu to create a bunch of unit tests. When I click "Test -Start Selected Test Project with Debugger" the tests run. I'd really like to create a WinForm User Interface to run the Unit Tests. The idea would be to give the user a list of Unit Tests in a table, and let the user select any combination of tests they like. The user then...
2
2603
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post by:
I was wonder if there is any protocol or suggestions on how to setup unit testing projects in large solution??? If you have a large solution of say 50 projects and add a project for every unit test it could get overwhelming - is there some standards on how to set this up??? Has anyone integrated this into Continuous Integration ???
0
8957
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8783
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9459
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9321
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9248
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6745
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6043
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4555
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.