这个测试名称是不是有点夸张了
正如标题所暗示的那样,这个测试名称只是上面的一点点吗?
WhenChargeIsGreaterThanRestingChargeButLessThanChargeRestApproachStep_OnUpdate_ChargeIsSetToRestingCharge
关于如何改进这一点有什么建议吗?或者就这样好吗?
下面是整个测试装置,因此您可以了解一些背景信息:)
public class NeuronTests
{
[Fact]
public void OnUpdate_NeuronFiresWhenChargeIsEqualToThreshold()
{
Neuron neuron = new Neuron();
bool fired = false;
neuron.Fired += (s, e) => fired = true;
neuron.Charge = Neuron.ChargeThreshold;
neuron.Update();
Assert.True(fired, "Neuron didn't fire");
}
[Fact]
public void OnUpdate_NeuronDoesntFireWhenChargeIsLessThanThreshold()
{
Neuron neuron = new Neuron();
bool fired = false;
neuron.Fired += (s, e) => fired = true;
neuron.Charge = Neuron.ChargeThreshold - 1f;
neuron.Update();
Assert.False(fired, "Neuron fired!");
}
[Fact]
public void OnUpdate_NeuronFiresWhenChargeIsGreaterThanThreshold()
{
Neuron neuron = new Neuron();
bool fired = false;
neuron.Fired += (s, e) => fired = true;
neuron.Charge = Neuron.ChargeThreshold + 1f;
neuron.Update();
Assert.True(fired, "Neuron didn't fire");
}
[Fact]
public void WhenNeuronFires_ChargeResetsToRestingCharge()
{
Neuron neuron = new Neuron();
neuron.Charge = Neuron.ChargeThreshold;
neuron.Update();
Assert.Equal(Neuron.RestingCharge, neuron.Charge);
}
[Fact]
public void AfterFiring_OnUpdate_NeuronWontFire()
{
Neuron neuron = new Neuron();
int fireCount = 0;
neuron.Fired += (s, e) => fireCount++;
neuron.Charge = Neuron.ChargeThreshold;
neuron.Update();
neuron.Charge = Neuron.ChargeThreshold;
neuron.Update();
Assert.Equal(1, fireCount);
}
[Fact]
public void WhenResting_OnUpdate_NeuronWillFire()
{
Neuron neuron = new Neuron();
int fireCount = 0;
neuron.Fired += (s, e) => fireCount++;
neuron.Charge = Neuron.ChargeThreshold;
neuron.Update();
neuron.Charge = Neuron.ChargeThreshold;
neuron.Update();
neuron.Charge = Neuron.ChargeThreshold;
neuron.Update();
Assert.Equal(2, fireCount);
}
[Fact]
public void WhenChargeIsGreaterThanRestingCharge_OnUpdate_ChargeDecreasesTowardsRestingCharge()
{
Neuron neuron = new Neuron();
neuron.Charge = Neuron.RestingCharge + (2 * Neuron.ChargeRestApproachStep);
neuron.Update();
Assert.Equal(Neuron.RestingCharge + Neuron.ChargeRestApproachStep, neuron.Charge);
}
[Fact]
public void WhenChargeIsGreaterThanRestingChargeButLessThanChargeRestApproachStep_OnUpdate_ChargeIsSetToRestingCharge()
{
Neuron neuron = new Neuron();
neuron.Charge = Neuron.RestingCharge + (Neuron.ChargeRestApproachStep * 0.5f);
neuron.Update();
Assert.Equal(Neuron.RestingCharge, neuron.Charge);
}
}
As the title suggests, is this test name just a little of the top?
WhenChargeIsGreaterThanRestingChargeButLessThanChargeRestApproachStep_OnUpdate_ChargeIsSetToRestingCharge
Any suggestions on how to improve this? or is it fine as it is?
Below is the whole test fixture as it stands so you can get some context :)
public class NeuronTests
{
[Fact]
public void OnUpdate_NeuronFiresWhenChargeIsEqualToThreshold()
{
Neuron neuron = new Neuron();
bool fired = false;
neuron.Fired += (s, e) => fired = true;
neuron.Charge = Neuron.ChargeThreshold;
neuron.Update();
Assert.True(fired, "Neuron didn't fire");
}
[Fact]
public void OnUpdate_NeuronDoesntFireWhenChargeIsLessThanThreshold()
{
Neuron neuron = new Neuron();
bool fired = false;
neuron.Fired += (s, e) => fired = true;
neuron.Charge = Neuron.ChargeThreshold - 1f;
neuron.Update();
Assert.False(fired, "Neuron fired!");
}
[Fact]
public void OnUpdate_NeuronFiresWhenChargeIsGreaterThanThreshold()
{
Neuron neuron = new Neuron();
bool fired = false;
neuron.Fired += (s, e) => fired = true;
neuron.Charge = Neuron.ChargeThreshold + 1f;
neuron.Update();
Assert.True(fired, "Neuron didn't fire");
}
[Fact]
public void WhenNeuronFires_ChargeResetsToRestingCharge()
{
Neuron neuron = new Neuron();
neuron.Charge = Neuron.ChargeThreshold;
neuron.Update();
Assert.Equal(Neuron.RestingCharge, neuron.Charge);
}
[Fact]
public void AfterFiring_OnUpdate_NeuronWontFire()
{
Neuron neuron = new Neuron();
int fireCount = 0;
neuron.Fired += (s, e) => fireCount++;
neuron.Charge = Neuron.ChargeThreshold;
neuron.Update();
neuron.Charge = Neuron.ChargeThreshold;
neuron.Update();
Assert.Equal(1, fireCount);
}
[Fact]
public void WhenResting_OnUpdate_NeuronWillFire()
{
Neuron neuron = new Neuron();
int fireCount = 0;
neuron.Fired += (s, e) => fireCount++;
neuron.Charge = Neuron.ChargeThreshold;
neuron.Update();
neuron.Charge = Neuron.ChargeThreshold;
neuron.Update();
neuron.Charge = Neuron.ChargeThreshold;
neuron.Update();
Assert.Equal(2, fireCount);
}
[Fact]
public void WhenChargeIsGreaterThanRestingCharge_OnUpdate_ChargeDecreasesTowardsRestingCharge()
{
Neuron neuron = new Neuron();
neuron.Charge = Neuron.RestingCharge + (2 * Neuron.ChargeRestApproachStep);
neuron.Update();
Assert.Equal(Neuron.RestingCharge + Neuron.ChargeRestApproachStep, neuron.Charge);
}
[Fact]
public void WhenChargeIsGreaterThanRestingChargeButLessThanChargeRestApproachStep_OnUpdate_ChargeIsSetToRestingCharge()
{
Neuron neuron = new Neuron();
neuron.Charge = Neuron.RestingCharge + (Neuron.ChargeRestApproachStep * 0.5f);
neuron.Update();
Assert.Equal(Neuron.RestingCharge, neuron.Charge);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
布局此类测试的一种流行方法是使用具有 Give/When/Then 类型词汇表的嵌套类,如典型 BDD 实践所建议的那样,例如,
通过这种方式,您还可以嵌套也适合
GivenChargeIsGreaterThanRestingCharge
的其他测试> 故事情节在同一个地方。One popular way to layout tests like these is to use nested classes with a Given/When/Then type vocabulary as suggested by typical BDD practices, e.g.
This way you can also nest other tests which also fit into the
GivenChargeIsGreaterThanRestingCharge
storyline in the same place.我个人的观点是,方法名称永远不会太长,只要它们具有描述性即可。
单元测试名称往往会更长,因为它们必须包含更多信息。这对我来说也很好,因为它们只出现在方法签名和测试列表中(这是您想要有一个好名字的地方),您永远不会从任何其他代码中调用它们。
My personal opinion is that method names can never be too long, as long as they are descriptive.
Unit test names tend to be a lot longer, since they have to contain more information. This is fine for me too, since they only appear in the method signature and in your list of tests (and this is where you want to have a good name), you'll never call them from any other code.
下划线提供了您认为应该从方法名称中移出的内容的线索。
那么你可以:
The underscores give clues to what you think should be moved out of the method name.
Then you could have:
它有点长,维护者希望阅读该函数以快速了解该函数的功能,拥有这么长的内容可以更快地阅读该函数本身。
这也适用于测试。当我觉得需要写一篇文章作为函数标题时,我会删除“何时”“是”并重复单词...留下:
ChargeGreaterThanRestingButLessThanRestApproachStep_OnUpdate_ChargeSetToResting
描述性并没有那么少,更容易阅读...
就像Windows Phone 7广告一样说“更多浏览并继续”
It is a bit long, maintainers want to read the function to get a quick idea what the function does, having something that long makes it quicker to read the function itself.
This applies to tests also. When I feel the need to write a essay as a function title, I strip out 'When' 'Is' and repeated words... leaving:
ChargeGreaterThanRestingButLessThanRestApproachStep_OnUpdate_ChargeSetToResting
Not much less descriptive, much more easily readable...
As The Windows Phone 7 Ads say 'More Glance and Go'
顺便说一句,命名测试的一种方法(当然不是唯一的方法)是将测试名称写为断言。
一个简单(幼稚)的例子:
在主要问题上,我认为长测试函数名称很好,只要它们是明确的
As an aside, one way (certainly not THE one way) of naming tests is to write your test name as an assertion.
A simple (naive) example:
On the main question I think long test function names are fine, as long as they're unambiguous