12

正如标题所暗示的,这个测试名称只是顶部的一小部分吗?

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);
        }


    }
4

5 回答 5

20

一种流行的布局测试的方法是使用带有 Given/When/Then 类型词汇表的嵌套类,如典型 BDD 实践所建议的那样,例如

public class NeuronStory
{
    public class GivenChargeIsGreaterThanRestingCharge
    {
        public class GivenChargeIsLessThanChargeRestApproachStep
        {
            public class WhenUpdated
            {
                public void ThenChargeIsSetToRestingCharge()
                {
                }
            }
        }
    }
}

这样,您还可以GivenChargeIsGreaterThanRestingCharge在同一位置嵌套其他也适合故事情节的测试。

于 2010-11-10T13:13:01.957 回答
15

我个人的观点是方法名称永远不能太长,只要它们是描述性的。

单元测试名称往往更长,因为它们必须包含更多信息。这对我来说也很好,因为它们只出现在方法签名和你的测试列表中(这是你想要一个好名字的地方),你永远不会从任何其他代码中调用它们。

于 2010-11-10T13:01:57.557 回答
3

下划线为您认为应该从方法名称中移出的内容提供线索。

  • 将正在测试的内容移至类名。
  • 将测试结果应该是什么移动到断言语句(必要时进行注释)。为什么?如果测试中的断言改变了,测试的名称应该改变吗?

然后你可以有:

public class NeuronOnUpdateTests
{
  public void WhenChargeIsBetweenRestingChargeAndChargeRestApproachStep
  {
    //Charge is set to resting state
    Assert.True(x);
  }
}
于 2010-11-10T15:05:27.873 回答
1

它有点长,维护人员想要阅读函数以快速了解函数的作用,拥有较长的内容可以更快地阅读函数本身。

这也适用于测试。当我觉得有必要写一篇作为函数标题的文章时,我会去掉'When''Is'和重复的单词......离开:

ChargeGreaterThanRestingButLessThanRestApproachStep_OnUpdate_ChargeSetToResting

不那么描述性,更容易阅读......

正如 Windows Phone 7 广告所说的“更多一目了然”

于 2010-11-10T13:22:37.663 回答
1

顺便说一句,命名测试的一种方法(当然不是一种方法)是将您的测试名称写为断言。

一个简单(天真)的例子:

int Add(object a, object b)
{
   return a+b;
}

[TestMethod]
void AddFailsWithNonIntegerArguments()
{
    try
    {
      Add("Hello", "World");
      Assert::Fail();
    }
    catch
    {
      Assert::Pass();
    }
}

关于主要问题,我认为长测试函数名称很好,只要它们是明确的

于 2010-11-10T15:53:42.283 回答