这个测试名称是不是有点夸张了

发布于 2024-10-02 09:52:19 字数 3442 浏览 5 评论 0原文

正如标题所暗示的那样,这个测试名称只是上面的一点点吗?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

要走就滚别墨迹 2024-10-09 09:52:19

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

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

通过这种方式,您还可以嵌套也适合 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.

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

This way you can also nest other tests which also fit into the GivenChargeIsGreaterThanRestingCharge storyline in the same place.

谜兔 2024-10-09 09:52:19

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

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

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.

宣告ˉ结束 2024-10-09 09:52:19

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

  • 将正在测试的内容移至类名。
  • 将测试结果移至断言语句中(如有必要,请进行注释)。为什么?如果测试中的断言发生变化,测试的名称是否应该更改?

那么你可以:

public class NeuronOnUpdateTests
{
  public void WhenChargeIsBetweenRestingChargeAndChargeRestApproachStep
  {
    //Charge is set to resting state
    Assert.True(x);
  }
}

The underscores give clues to what you think should be moved out of the method name.

  • Move what is under test to the class name.
  • Move what the test result should be to the assert statement (comment if necessary). Why? If the assertion in the test ever changes, should the name of the test change?

Then you could have:

public class NeuronOnUpdateTests
{
  public void WhenChargeIsBetweenRestingChargeAndChargeRestApproachStep
  {
    //Charge is set to resting state
    Assert.True(x);
  }
}
轻拂→两袖风尘 2024-10-09 09:52:19

它有点长,维护者希望阅读该函数以快速了解该函数的功能,拥有这么长的内容可以更快地阅读该函数本身。

这也适用于测试。当我觉得需要写一篇文章作为函数标题时,我会删除“何时”“是”并重复单词...留下:

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'

扛刀软妹 2024-10-09 09:52:19

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

一个简单(幼稚)的例子:

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

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

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

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:

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

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

On the main question I think long test function names are fine, as long as they're unambiguous

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文