473,480 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Create 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=CheckType, Args="-1,3,'flower'", Result=true]

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

[Test, Function=CheckType, 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 2531
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 MyAdditionObject which is
used to add two numbers, to test it (using NUnit) you would write something
like:

using NUnit.Framework;

[TestFixture]
public class MyAdditionObjectTestFixture
{
[Test]
public void TestAddNumbers()
{
MyAdditionObject myAddObj = new MyAdditionObject();

int intResult = myAddObj.AddTwoNumbers(2,3);

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

[Test]
public void TestAddNegativePositiveNumber()
{
MyAdditionObject myAddObj = new MyAdditionObject();

int intResult = myAddObj.AddTwoNumbers(-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=CheckType, Args="-1,3,'flower'", Result=true]

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

[Test, Function=CheckType, 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*********@discussions.microsoft.com> wrote in message
news:AF**********************************@microsof t.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 MyAdditionObject which is
used to add two numbers, to test it (using NUnit) you would write
something
like:

using NUnit.Framework;

[TestFixture]
public class MyAdditionObjectTestFixture
{
[Test]
public void TestAddNumbers()
{
MyAdditionObject myAddObj = new MyAdditionObject();

int intResult = myAddObj.AddTwoNumbers(2,3);

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

[Test]
public void TestAddNegativePositiveNumber()
{
MyAdditionObject myAddObj = new MyAdditionObject();

int intResult = myAddObj.AddTwoNumbers(-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=CheckType, Args="-1,3,'flower'", Result=true]

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

[Test, Function=CheckType, 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 MyAdditionObject which is
used to add two numbers, to test it (using NUnit) you would write something
like:

using NUnit.Framework;

[TestFixture]
public class MyAdditionObjectTestFixture
{
[Test]
public void TestAddNumbers()
{
MyAdditionObject myAddObj = new MyAdditionObject();

int intResult = myAddObj.AddTwoNumbers(2,3);

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

[Test]
public void TestAddNegativePositiveNumber()
{
MyAdditionObject myAddObj = new MyAdditionObject();

int intResult = myAddObj.AddTwoNumbers(-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=CheckType, Args="-1,3,'flower'", Result=true]

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

[Test, Function=CheckType, 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*********@discussions.microsoft.com> wrote in message
news:AF**********************************@microsof t.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 MyAdditionObject which is
used to add two numbers, to test it (using NUnit) you would write
something
like:

using NUnit.Framework;

[TestFixture]
public class MyAdditionObjectTestFixture
{
[Test]
public void TestAddNumbers()
{
MyAdditionObject myAddObj = new MyAdditionObject();

int intResult = myAddObj.AddTwoNumbers(2,3);

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

[Test]
public void TestAddNegativePositiveNumber()
{
MyAdditionObject myAddObj = new MyAdditionObject();

int intResult = myAddObj.AddTwoNumbers(-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=CheckType, Args="-1,3,'flower'", Result=true]

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

[Test, Function=CheckType, 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=CheckType, Args="-1,3,'flower'", Result=true]

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

[Test, Function=CheckType, 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=CheckType, Args="-1,3,'flower'", Result=true]

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

[Test, Function=CheckType, 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*********@NOSPAMLOWLIFEcitect.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.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #8
Greg Roberts <gr*********@NOSPAMLOWLIFEcitect.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.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #9
Helge Jensen <he**********@slog.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.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #10
Helge Jensen <he**********@slog.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.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #11


Jon Skeet [C# MVP] wrote:
Helge Jensen <he**********@slog.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?


nope, don't know much about nUnit, It was just my impression from the
docs i've read. Should have added ", doesn't it?".

I have (too) extensive experience with a C++ framework called xTest, and
jUnit, ...Fixtures and Suites, setUp, tearDown,... it's all coming back
in my dreams.
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?


Especially this feature in particular, but also the frameworks in general.

Here it comes, some of it at least :)

unit-testing is *good*, it works and finds problems. how unit-test
*frameworks* are helping with that is unclear to me.

Tests are no different from any other code and should be written as
such. Test-code should not be inserted in "hot-spots" in a
test-framework -- People bend their test-code to the wierd syntax and
convetions required by the frameworks, atleast that's what i've seen happen.

Instead a test-*library* should provide some handy functionality, like
run_check(f, 1, 2, expected_result); and run_expect(f, 1, 3,
typeof(IndexOutOfRangeException)), which will become the "test-protocol".

This is difficult in (at least) < C#2.0, since

- you need to explicitly declare and instantiate a callback that fits
the type of f
- you can not catch by Type (the type Type, understandable?), only by
"exception-declaration"

This may have changed in C#2.0 but I haven't moved on from 1.1 yet. I
have a faint hope (from rumours and hear-say) that I will be able to use
the generics in C#2 and the implicit delegate support to implement such
a test-library. I have done it before, in C++ (thanks to the *amazing*
boost::function) and it works like a charm.

In the mean time I have made the following observations:

There are good ways to sequentially compose operations in C#:

op1(); op2()

It is more complicated to independently compose execution, but
relatively easy in most cases:

new ThreadStart(op1).Start();
new ThreadStart(op2).Start();

Exceptions are a *blazingly* good way of signalling that something is wrong:

void CHECK(boolean b) { throw new CheckExpection(); }

and the stack-trace tells you *exactly* what function failed, you can
then rerun the test in question setting a break-point at the failed line.

If you are running in debug-mode you can sneakily set a breakpoint in
CheckException.CheckException().

I don't use failure-counts anyway, it's either 0 or !0

If I want to run a specific subset of tests:

class SpecificSubsetOfTests {
public static int Main() {
#if !DEBUG
try {
#endif
// just run the tests
#if !DEBUG
} catch ( Exception e ) {
Console.WriteLine("EXCEPTION:\n{0}", e);
throw;
}
#endif
}
}

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


Jon Skeet [C# MVP] wrote:
Helge Jensen <he**********@slog.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?


nope, don't know much about nUnit, It was just my impression from the
docs i've read. Should have added ", doesn't it?".

I have (too) extensive experience with a C++ framework called xTest, and
jUnit, ...Fixtures and Suites, setUp, tearDown,... it's all coming back
in my dreams.
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?


Especially this feature in particular, but also the frameworks in general.

Here it comes, some of it at least :)

unit-testing is *good*, it works and finds problems. how unit-test
*frameworks* are helping with that is unclear to me.

Tests are no different from any other code and should be written as
such. Test-code should not be inserted in "hot-spots" in a
test-framework -- People bend their test-code to the wierd syntax and
convetions required by the frameworks, atleast that's what i've seen happen.

Instead a test-*library* should provide some handy functionality, like
run_check(f, 1, 2, expected_result); and run_expect(f, 1, 3,
typeof(IndexOutOfRangeException)), which will become the "test-protocol".

This is difficult in (at least) < C#2.0, since

- you need to explicitly declare and instantiate a callback that fits
the type of f
- you can not catch by Type (the type Type, understandable?), only by
"exception-declaration"

This may have changed in C#2.0 but I haven't moved on from 1.1 yet. I
have a faint hope (from rumours and hear-say) that I will be able to use
the generics in C#2 and the implicit delegate support to implement such
a test-library. I have done it before, in C++ (thanks to the *amazing*
boost::function) and it works like a charm.

In the mean time I have made the following observations:

There are good ways to sequentially compose operations in C#:

op1(); op2()

It is more complicated to independently compose execution, but
relatively easy in most cases:

new ThreadStart(op1).Start();
new ThreadStart(op2).Start();

Exceptions are a *blazingly* good way of signalling that something is wrong:

void CHECK(boolean b) { throw new CheckExpection(); }

and the stack-trace tells you *exactly* what function failed, you can
then rerun the test in question setting a break-point at the failed line.

If you are running in debug-mode you can sneakily set a breakpoint in
CheckException.CheckException().

I don't use failure-counts anyway, it's either 0 or !0

If I want to run a specific subset of tests:

class SpecificSubsetOfTests {
public static int Main() {
#if !DEBUG
try {
#endif
// just run the tests
#if !DEBUG
} catch ( Exception e ) {
Console.WriteLine("EXCEPTION:\n{0}", e);
throw;
}
#endif
}
}

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Nov 17 '05 #13
It took me a while to figure out how to use
the VS 2005 beta 2 built-in Team TestFramework,
but I'm fond of it now. Still exploring it; the walkthroughs
demo _some_ of the capability, especially the
code it generates to test private methods.

Worth d/ling the beta to experiment with, imo.
--
Grace + Peace,
Peter N Roth
Engineering Objects International
http://engineeringobjects.com
Home of Matrix.NET
"Helge Jensen" <he**********@slog.dk> wrote in message
news:43**************@slog.dk...


Jon Skeet [C# MVP] wrote:
Helge Jensen <he**********@slog.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?


nope, don't know much about nUnit, It was just my impression from the docs
i've read. Should have added ", doesn't it?".

I have (too) extensive experience with a C++ framework called xTest, and
jUnit, ...Fixtures and Suites, setUp, tearDown,... it's all coming back in
my dreams.
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?


Especially this feature in particular, but also the frameworks in general.

Here it comes, some of it at least :)

unit-testing is *good*, it works and finds problems. how unit-test
*frameworks* are helping with that is unclear to me.

Tests are no different from any other code and should be written as such.
Test-code should not be inserted in "hot-spots" in a test-framework --
People bend their test-code to the wierd syntax and convetions required by
the frameworks, atleast that's what i've seen happen.

Instead a test-*library* should provide some handy functionality, like
run_check(f, 1, 2, expected_result); and run_expect(f, 1, 3,
typeof(IndexOutOfRangeException)), which will become the "test-protocol".

This is difficult in (at least) < C#2.0, since

- you need to explicitly declare and instantiate a callback that fits
the type of f
- you can not catch by Type (the type Type, understandable?), only by
"exception-declaration"

This may have changed in C#2.0 but I haven't moved on from 1.1 yet. I have
a faint hope (from rumours and hear-say) that I will be able to use the
generics in C#2 and the implicit delegate support to implement such a
test-library. I have done it before, in C++ (thanks to the *amazing*
boost::function) and it works like a charm.

In the mean time I have made the following observations:

There are good ways to sequentially compose operations in C#:

op1(); op2()

It is more complicated to independently compose execution, but relatively
easy in most cases:

new ThreadStart(op1).Start();
new ThreadStart(op2).Start();

Exceptions are a *blazingly* good way of signalling that something is
wrong:

void CHECK(boolean b) { throw new CheckExpection(); }

and the stack-trace tells you *exactly* what function failed, you can then
rerun the test in question setting a break-point at the failed line.

If you are running in debug-mode you can sneakily set a breakpoint in
CheckException.CheckException().

I don't use failure-counts anyway, it's either 0 or !0

If I want to run a specific subset of tests:

class SpecificSubsetOfTests {
public static int Main() {
#if !DEBUG
try {
#endif
// just run the tests
#if !DEBUG
} catch ( Exception e ) {
Console.WriteLine("EXCEPTION:\n{0}", e);
throw;
}
#endif
}
}

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-

Nov 17 '05 #14
It took me a while to figure out how to use
the VS 2005 beta 2 built-in Team TestFramework,
but I'm fond of it now. Still exploring it; the walkthroughs
demo _some_ of the capability, especially the
code it generates to test private methods.

Worth d/ling the beta to experiment with, imo.
--
Grace + Peace,
Peter N Roth
Engineering Objects International
http://engineeringobjects.com
Home of Matrix.NET
"Helge Jensen" <he**********@slog.dk> wrote in message
news:43**************@slog.dk...


Jon Skeet [C# MVP] wrote:
Helge Jensen <he**********@slog.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?


nope, don't know much about nUnit, It was just my impression from the docs
i've read. Should have added ", doesn't it?".

I have (too) extensive experience with a C++ framework called xTest, and
jUnit, ...Fixtures and Suites, setUp, tearDown,... it's all coming back in
my dreams.
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?


Especially this feature in particular, but also the frameworks in general.

Here it comes, some of it at least :)

unit-testing is *good*, it works and finds problems. how unit-test
*frameworks* are helping with that is unclear to me.

Tests are no different from any other code and should be written as such.
Test-code should not be inserted in "hot-spots" in a test-framework --
People bend their test-code to the wierd syntax and convetions required by
the frameworks, atleast that's what i've seen happen.

Instead a test-*library* should provide some handy functionality, like
run_check(f, 1, 2, expected_result); and run_expect(f, 1, 3,
typeof(IndexOutOfRangeException)), which will become the "test-protocol".

This is difficult in (at least) < C#2.0, since

- you need to explicitly declare and instantiate a callback that fits
the type of f
- you can not catch by Type (the type Type, understandable?), only by
"exception-declaration"

This may have changed in C#2.0 but I haven't moved on from 1.1 yet. I have
a faint hope (from rumours and hear-say) that I will be able to use the
generics in C#2 and the implicit delegate support to implement such a
test-library. I have done it before, in C++ (thanks to the *amazing*
boost::function) and it works like a charm.

In the mean time I have made the following observations:

There are good ways to sequentially compose operations in C#:

op1(); op2()

It is more complicated to independently compose execution, but relatively
easy in most cases:

new ThreadStart(op1).Start();
new ThreadStart(op2).Start();

Exceptions are a *blazingly* good way of signalling that something is
wrong:

void CHECK(boolean b) { throw new CheckExpection(); }

and the stack-trace tells you *exactly* what function failed, you can then
rerun the test in question setting a break-point at the failed line.

If you are running in debug-mode you can sneakily set a breakpoint in
CheckException.CheckException().

I don't use failure-counts anyway, it's either 0 or !0

If I want to run a specific subset of tests:

class SpecificSubsetOfTests {
public static int Main() {
#if !DEBUG
try {
#endif
// just run the tests
#if !DEBUG
} catch ( Exception e ) {
Console.WriteLine("EXCEPTION:\n{0}", e);
throw;
}
#endif
}
}

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-

Nov 17 '05 #15
Helge Jensen <he**********@slog.dk> wrote:
nUnit does stuff like this. I haven't seen anything in NUnit for that - could you point me at a
reference?


nope, don't know much about nUnit, It was just my impression from the
docs i've read. Should have added ", doesn't it?".


Right - no, I don't believe it does.
I have (too) extensive experience with a C++ framework called xTest, and
jUnit, ...Fixtures and Suites, setUp, tearDown,... it's all coming back
in my dreams.
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?


Especially this feature in particular, but also the frameworks in general.

Here it comes, some of it at least :)

unit-testing is *good*, it works and finds problems. how unit-test
*frameworks* are helping with that is unclear to me.


They make it very easy to write tests and then run them in an automated
manner, collecting the output in a nice form which can be attached to
build reports, etc.

How do you normally *run* your tests? With Eclipse and JUnit, I can do
it all within the IDE, debug into it easily, run as many or as few
tests as I want to etc. We have thousands of test cases over hundreds
of classes. Without using a test framework, how can I get the same
level of flexibility?
Tests are no different from any other code and should be written as
such. Test-code should not be inserted in "hot-spots" in a
test-framework -- People bend their test-code to the wierd syntax and
convetions required by the frameworks, atleast that's what i've seen happen.
Not sure what you mean here. There's no particularly weird syntax to
JUnit or NUnit - they're just straight Java or C#, potentially using
base classes which are provided by the framework.
Instead a test-*library* should provide some handy functionality, like
run_check(f, 1, 2, expected_result); and run_expect(f, 1, 3,
typeof(IndexOutOfRangeException)), which will become the "test-protocol".
Neither of those are particularly hard to do in NUnit or JUnit.
This is difficult in (at least) < C#2.0, since

- you need to explicitly declare and instantiate a callback that fits
the type of f
- you can not catch by Type (the type Type, understandable?), only by
"exception-declaration"

This may have changed in C#2.0 but I haven't moved on from 1.1 yet. I
have a faint hope (from rumours and hear-say) that I will be able to use
the generics in C#2 and the implicit delegate support to implement such
a test-library. I have done it before, in C++ (thanks to the *amazing*
boost::function) and it works like a charm.

In the mean time I have made the following observations:

There are good ways to sequentially compose operations in C#:

op1(); op2()

It is more complicated to independently compose execution, but
relatively easy in most cases:

new ThreadStart(op1).Start();
new ThreadStart(op2).Start();

Exceptions are a *blazingly* good way of signalling that something is wrong:

void CHECK(boolean b) { throw new CheckExpection(); }

and the stack-trace tells you *exactly* what function failed, you can
then rerun the test in question setting a break-point at the failed line.
And guess what - that's exactly what JUnit and NUnit do.
If you are running in debug-mode you can sneakily set a breakpoint in
CheckException.CheckException().

I don't use failure-counts anyway, it's either 0 or !0
Whereas I don't want to have to run the whole test suite again after
fixing one test, only to find there's another one that's failing - and
repeat that for as many tests as are failing. Usually there aren't many
(or even any) failing tests, but I still want to have a confidence
level about the rest of the code even if one fails.

Currently we've got four tests which are failing, and have been failing
for a few days while we try to find a solution. Would you suggest that
it's pointless knowing whether any *other* tests (including new ones)
have started failing over those last few days?
If I want to run a specific subset of tests:

class SpecificSubsetOfTests {
public static int Main() {
#if !DEBUG
try {
#endif
// just run the tests
#if !DEBUG
} catch ( Exception e ) {
Console.WriteLine("EXCEPTION:\n{0}", e);
throw;
}
#endif
}
}


Whereas I can just select a package in Eclipse, and run all the tests
within that package and sub-packages. I've never needed to specify in
code a list of tests to run.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #16
Helge Jensen <he**********@slog.dk> wrote:
nUnit does stuff like this. I haven't seen anything in NUnit for that - could you point me at a
reference?


nope, don't know much about nUnit, It was just my impression from the
docs i've read. Should have added ", doesn't it?".


Right - no, I don't believe it does.
I have (too) extensive experience with a C++ framework called xTest, and
jUnit, ...Fixtures and Suites, setUp, tearDown,... it's all coming back
in my dreams.
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?


Especially this feature in particular, but also the frameworks in general.

Here it comes, some of it at least :)

unit-testing is *good*, it works and finds problems. how unit-test
*frameworks* are helping with that is unclear to me.


They make it very easy to write tests and then run them in an automated
manner, collecting the output in a nice form which can be attached to
build reports, etc.

How do you normally *run* your tests? With Eclipse and JUnit, I can do
it all within the IDE, debug into it easily, run as many or as few
tests as I want to etc. We have thousands of test cases over hundreds
of classes. Without using a test framework, how can I get the same
level of flexibility?
Tests are no different from any other code and should be written as
such. Test-code should not be inserted in "hot-spots" in a
test-framework -- People bend their test-code to the wierd syntax and
convetions required by the frameworks, atleast that's what i've seen happen.
Not sure what you mean here. There's no particularly weird syntax to
JUnit or NUnit - they're just straight Java or C#, potentially using
base classes which are provided by the framework.
Instead a test-*library* should provide some handy functionality, like
run_check(f, 1, 2, expected_result); and run_expect(f, 1, 3,
typeof(IndexOutOfRangeException)), which will become the "test-protocol".
Neither of those are particularly hard to do in NUnit or JUnit.
This is difficult in (at least) < C#2.0, since

- you need to explicitly declare and instantiate a callback that fits
the type of f
- you can not catch by Type (the type Type, understandable?), only by
"exception-declaration"

This may have changed in C#2.0 but I haven't moved on from 1.1 yet. I
have a faint hope (from rumours and hear-say) that I will be able to use
the generics in C#2 and the implicit delegate support to implement such
a test-library. I have done it before, in C++ (thanks to the *amazing*
boost::function) and it works like a charm.

In the mean time I have made the following observations:

There are good ways to sequentially compose operations in C#:

op1(); op2()

It is more complicated to independently compose execution, but
relatively easy in most cases:

new ThreadStart(op1).Start();
new ThreadStart(op2).Start();

Exceptions are a *blazingly* good way of signalling that something is wrong:

void CHECK(boolean b) { throw new CheckExpection(); }

and the stack-trace tells you *exactly* what function failed, you can
then rerun the test in question setting a break-point at the failed line.
And guess what - that's exactly what JUnit and NUnit do.
If you are running in debug-mode you can sneakily set a breakpoint in
CheckException.CheckException().

I don't use failure-counts anyway, it's either 0 or !0
Whereas I don't want to have to run the whole test suite again after
fixing one test, only to find there's another one that's failing - and
repeat that for as many tests as are failing. Usually there aren't many
(or even any) failing tests, but I still want to have a confidence
level about the rest of the code even if one fails.

Currently we've got four tests which are failing, and have been failing
for a few days while we try to find a solution. Would you suggest that
it's pointless knowing whether any *other* tests (including new ones)
have started failing over those last few days?
If I want to run a specific subset of tests:

class SpecificSubsetOfTests {
public static int Main() {
#if !DEBUG
try {
#endif
// just run the tests
#if !DEBUG
} catch ( Exception e ) {
Console.WriteLine("EXCEPTION:\n{0}", e);
throw;
}
#endif
}
}


Whereas I can just select a package in Eclipse, and run all the tests
within that package and sub-packages. I've never needed to specify in
code a list of tests to run.

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

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

Similar topics

38
3357
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...
14
2705
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
2093
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...
5
3900
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...
72
5150
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: ...
28
1445
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? ...
176
8179
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...
5
2829
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...
2
2582
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...
0
7039
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,...
0
7037
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,...
0
7080
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...
0
6895
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4770
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...
0
4476
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...
0
1296
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
558
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
176
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...

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.