C# - 属性 - 单元测试问题 [英] C# - Attributes - Unit Tests Question

查看:76
本文介绍了C# - 属性 - 单元测试问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我想使用属性在代码中放置所需的测试。好像

足够的代码片段

让我来掩盖这个。例如


//测试用例,在函数上运行这些并查看结​​果


[测试,函数= CheckType,Args =" ; -1,3,''flower''",Result = true]


[测试,功能= CheckType,Args =" 5,3,''pot'' ",Result = true]


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


//函数


static布尔CheckType(int FirstArg,int SecondArg,string sType)


{


//代码在这里


...


返回bResult;


}


更难的部分是我想用反射建立函数

参数并在


使用正确的值堆叠这些参数然后调用函数

捕获返回的值。


这将是太棒了作为不再需要2组代码需要维持,b $ b维持,i。 e。原始


函数然后运行这些函数的测试用例。


该函数的测试与函数一起使用。 />

你能否评论一下这是否可行?如果有一个很好的例子?



C#求职者


提前致谢

解决方案

嗨格雷格,

什么测试您使用框架进行单元测试,或者您是否想要自己编写



您不希望测试您的功能与您的生产

代码,因为这会增加额外的代码大小,并使代码更难以阅读

- 此外,您正在向最终客户发送测试代码 - 您应该分开

您正在测试的代码中的测试代码。即拥有所有测试代码

生活在与生产代码不同的项目中。


如果您还没有听说过NUnit( www.nunit.org )那么这将是非常好的

检查出来,它很简单但非常有效并且麻烦了你需要编写自己的测试框架。


你应该尝试每个动作编写一个测试函数你要测试,这个

的方式你不需要将参数传递给你的测试函数,并且你在初始化你要测试的对象的函数里面有
并通过在

对象的值。


例如一些名为MyAdditionObject的虚构对象

用于添加两个数字,测试它(使用NUnit)你会写一些东西

喜欢:


使用NUnit.Framework;


[TestFixture]

公共课MyAdditionObjectTestFixture

{

[测试]

public void TestAddNumbers()

{

MyAdditionObject myAddObj = new MyAdditionObject();


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


//检查结果是正确的,这是NUnit原生的对象

Assert.AreEqual(5,intResult);

}


[测试]

public void TestAddNegativePositiveNumber()

{

MyAdditionObject myAddObj = new MyAdditionObject();


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


//检查结果是否正确,这是NUnit原生的对象

Assert .AreEqual(1,intResult);

}

}


您可以在测试功能中看到您的参数测试

函数是无参数的。


希望这有帮助

Mark R Dawson

" Greg罗伯茨"写道:



我想使用属性在代码中放置所需的测试。
似乎有足够的代码片段来覆盖这个。例如

//测试用例,在函数上运行这些并检查结果

[测试,函数= 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]

//功能

静态Boolean CheckType(int FirstArg,int SecondArg,string sType)

//代码在这里



返回bResult;



更难的部分是我想用反射建立功能
参数并在
上创建
使用正确的值堆叠这些参数,然后调用函数
捕获返回的值。

这将是太棒了,因为不再需要2组代码
维护,即原始功能然后运行的测试用例这些函数。

该函数的测试与函数一起使用。

你能否评论是否可行以及是否可以有一个很好的例子吗?



C#begineer

提前致谢



感谢您的反馈,更多信息


是的,我们正在使用MSTest,NUnit和其他一些旧的/新的C ++

(非托管)/ C#产品。


我们正在开始一个新的C#(主要)功能集,我正在研究什么是

可能的做最好的

测试设置。我对C#比较陌生。做了几个奇怪的工具。


目前如果你添加或更改一个功能,你需要去测试

framemowork并添加/调整

,在某些情况下由于测试中的开关/案例也需要

定义了新的数字。


To我有能力在C#中隐藏文本,大多数测试用例

与实际代码一起生活将是理想的。


因此这篇文章。


谢谢


" Mark R. Dawson" <马********* @ discussions.microsoft.com>在消息中写道

新闻:AF ********************************** @ microsof t.com ...

您好Greg,
您使用什么测试框架进行单元测试,或者您是否正在尝试编写自己的测试框架?
<您不希望您的功能测试与您的生产代码一起使用,因为这会增加额外的代码大小,并使代码更难以阅读
- 而且您是向最终客户发送测试代码 - 您应该从正在测试的代码中分离测试代码。即将所有测试代码生活在与生产代码不同的项目中。

如果您还没有听说过NUnit( www.nunit.org )那么检查这一点真的很好
这很简单但很有效并且需要麻烦
你必须编写自己的测试框架。

你应该尝试为你想要测试的每个动作编写一个测试函数,
这个/>方式你不需要将参数传递给你的测试函数,并且在函数内部初始化你想要测试的对象并将值传递给对象。
<例如,一些名为MyAdditionObject的虚构对象用于添加两个数字,用于测试它(使用NUnit)你会写一些像
这样的东西:

使用NUnit.Framework;

[TestFixture]
公共类MyAdditionObjectTestFixture
{
[Tes t]
public void TestAddNumbers()
MyAdditionObject myAddObj = new MyAdditionObject();
int intResult = myAddObj.AddTwoNumbers(2,3);
检查结果是否正确,这是NUnit原生的对象
Assert.AreEqual(5,intResult);
}

[测试]
public void TestAddNegativePositiveNumber()
MyAdditionObject myAddObj = new MyAdditionObject();

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

//检查结果是否正确,这是NUnit原生的对象
Assert.AreEqual(1,intResult);
}
}
您可以在测试功能中看到您的参数,并且所有测试功能都是无参数的。

希望这有帮助
Mark R Dawson

" Greg Roberts写道:



我想使用属性在代码中放置所需的测试。
似乎
足以让我覆盖这个代码片段。例如

//测试用例,在函数上运行这些并检查结果

[测试,函数= 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]

//功能

静态Boolean CheckType(int FirstArg,int SecondArg,string sType)

//代码在这里



返回bResult;



更难的部分是我想用反射建立功能
参数并在
上创建
使用正确的值堆叠这些参数,然后调用函数
捕获返回的值。

这将是太棒了,因为不再需要2组代码
维护,即原始功能然后运行的测试用例这些功能。

该功能的测试与该功能同时存在。

>>如果有可能并且有一个例子很好吗?



C#begineer

提前致谢



嗨Greg,

你用什么测试框架进行单元测试,或者你是

试着自己编写?


你不希望你的函数测试与你的生产代码一起生成

代码,因为这会增加额外的代码大小,并且使代码更难以阅读

- 此外,您正在向最终客户发送测试代码 - 您应该从正在测试的代码中分离您的测试代码。即拥有所有测试代码

生活在与生产代码不同的项目中。


如果您还没有听说过NUnit( www.nunit.org )那么这将是非常好的

检查出来,它很简单但非常有效并且麻烦了你需要编写自己的测试框架。


你应该尝试每个动作编写一个测试函数你要测试,这个

的方式你不需要将参数传递给你的测试函数,并且你在初始化你要测试的对象的函数里面有
并通过在

对象的值。


例如一些名为MyAdditionObject的虚构对象

用于添加两个数字,测试它(使用NUnit)你会写一些东西

喜欢:


使用NUnit.Framework;


[TestFixture]

公共课MyAdditionObjectTestFixture

{

[测试]

public void TestAddNumbers()

{

MyAdditionObject myAddObj = new MyAdditionObject();


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


//检查结果是正确的,这是NUnit原生的对象

Assert.AreEqual(5,intResult);

}


[测试]

public void TestAddNegativePositiveNumber()

{

MyAdditionObject myAddObj = new MyAdditionObject();


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


//检查结果是否正确,这是NUnit原生的对象

Assert .AreEqual(1,intResult);

}

}


您可以在测试功能中看到您的参数测试

函数是无参数的。


希望这有帮助

Mark R Dawson

" Greg罗伯茨"写道:



我想使用属性在代码中放置所需的测试。
似乎有足够的代码片段来覆盖这个。例如

//测试用例,在函数上运行这些并检查结果

[测试,函数= 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]

//功能

静态Boolean CheckType(int FirstArg,int SecondArg,string sType)

//代码在这里



返回bResult;



更难的部分是我想用反射建立功能
参数并在
上创建
使用正确的值堆叠这些参数,然后调用函数
捕获返回的值。

这将是太棒了,因为不再需要2组代码
维护,即原始功能然后运行的测试用例这些函数。

该函数的测试与函数一起使用。

你能否评论是否可行以及是否可以有一个很好的例子吗?



C#begineer

提前致谢



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

解决方案

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



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



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



这篇关于C# - 属性 - 单元测试问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆