代码之家  ›  专栏  ›  技术社区  ›  Sekhat

这个测试名称有点过头了吗

  •  12
  • Sekhat  · 技术社区  · 14 年前

    正如题目所暗示的,这个考试名字是不是有点名列前茅?

    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);
            }
    
    
        }
    
    5 回复  |  直到 14 年前
        1
  •  15
  •   Gerrie Schenck    14 年前

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

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

        2
  •  20
  •   Adam Ralph    14 年前

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

    这样,您还可以嵌套其他测试,这些测试也适合 GivenChargeIsGreaterThanRestingCharge 故事情节在同一个地方。

        3
  •  3
  •   Amy B    14 年前

    • 将正在测试的内容移动到类名。
    • 将测试结果移到assert语句中(必要时注释)。为什么?如果测试中的断言发生更改,测试的名称是否应该更改?

    public class NeuronOnUpdateTests
    {
      public void WhenChargeIsBetweenRestingChargeAndChargeRestApproachStep
      {
        //Charge is set to resting state
        Assert.True(x);
      }
    }
    
        4
  •  1
  •   stevenrcfox    14 年前

    它有点长,维护人员想通过阅读函数来快速了解函数的功能,如果有那么长的内容,就可以更快地阅读函数本身。

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

    不需要太多描述,更容易阅读。。。

    正如WindowsPhone7的广告所说的“更多的一目了然”

        5
  •  1
  •   CJBrew    14 年前

    另外,命名测试的一种方法(当然不是唯一的方法)是将测试名称作为断言编写。

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

    int Add(object a, object b)
    {
       return a+b;
    }
    
    [TestMethod]
    void AddFailsWithNonIntegerArguments()
    {
        try
        {
          Add("Hello", "World");
          Assert::Fail();
        }
        catch
        {
          Assert::Pass();
        }
    }
    

    在主要问题上,我认为长测试函数名是可以的,只要它们是明确的