English 中文(简体)
在测试方法中使用同样的固定价值有何利弊?
原标题:What are pros and cons of using same constant value in test method?
  • 时间:2012-05-20 14:25:34
  •  标签:
  • tdd
最佳回答

我认为你错误地解释这一说法。 作者(ho)试图表达的是,以下法典是灾害的对应法。

const SomeRandomValue = 32;
...
// Plus testcase
Plus(SomeRandomValue, SomeRandomValue)
...
// Divide testcase
Divide(SomeRandomValue, SomeRandomValue)

你们有两个试样,用none descriptive不变。 无法知道,通过将<代码>SomeRandomValue改为0,你的测试程序将失败。

更好的点名会像这样。

const AdditionValue = 32;
const DivisorValue = 32;
...
// Plus testcase
Plus(AdditionValue, AdditionValue)
...
// Divide testcase
Divide(DivisorValue, DivisorValue)

在什么地方,对固定装置的使用应当显而易见。 您在创建试验场时不应再使用密码。

或用其他词语表述:

I don t see anything wrong with reusing the DivisorValue constant in multiple testcases > but there is definitly something wrong trying to shoehorn one value into a none descriptive variable just in the name of code reuse.

问题回答

如果你在测试中使用同样的价值——如<条码>+(1、1)——那么你的代码就可出于错误原因发挥作用。 此处采用<代码> 附加<<>><>>>>>,通过这种测试,但测试结果不同。

public int Plus (int a, int b) {
    return a + a;
}

A test that avoids this risk is a better test than one which lets errors like these slip through.





相关问题
Separating rapid development from refactoring/optimization

I m working in a team of 2 front-end developers on a web-based late-stage startup project. The site works quite well, but there s a lot of room for improvement code-wise, as the code is quite messy ...

Test-driven development with ASP.NET MVC - where to begin?

I ve read a lot about Test-Driven Development (TDD) and I find the principles very compelling, based on personal experience. At the moment I m developing a website for a start-up project I m involved ...

Silencing Factory Girl logging

Just to clear the air, I am not some cruel factory master trying to silence working ladies. I am having a very annoying problem where when using Thoughtbot s factory girl in my specs, every time ...

TDD vs. Unit testing [closed]

My company is fairly new to unit testing our code. I ve been reading about TDD and unit testing for some time and am convinced of their value. I ve attempted to convince our team that TDD is worth ...

unit test a method that creates an object

I m trying to get my head round Unit Testing and there s one more piece of the jigsaw I need to find. What I m trying to do is write tests for the following code. In this case, I ve got a really ...

Testing private method of an abstract class using Reflection

How can I test a private method of an abstract class using reflection (using C#)? I am specifically interested in adapting the code found in this thread. I am aware of the discussion around the ...

C#: How would you unit test GetHashCode?

Testing the Equals method is pretty much straight forward (as far as I know). But how on earth do you test the GetHashCode method?

热门标签