集成测试时设置系统状态的最佳实践/想法?

发布于 2024-07-23 07:16:47 字数 601 浏览 5 评论 0原文

我有许多使用 Cucumber 流行的 Give/When/Then 风格编写的 C# 集成测试。 我使用的框架基本上与 NBehave 相同。

我面临的一个反复出现的问题是设置和连接集成测试所需的所有应用程序状态的问题。 我的大多数测试看起来像这样:

Given an empty system
  And a new NetworkServer
  And a new ServerDatabase
  And a new Eventlogger
  And a new Networkconnection
  And a new LoggingClient
When the client logs a new event
Then it should appear in the server database

如您所见,操作和断言是单行,但我有 6 行“接线”。 我的几乎每个测试都会重复这 6 行。

这对我来说似乎是一种代码味道,但我不确定如何处理这个问题。 我可以将这 6 行重构为一行(给定“一个有效的系统...” 或类似的内容),但这似乎太过分了,我会隐藏太多信息。

我很感激在这方面有更多经验的其他人的任何想法。 多谢。

I have a number of C# integration tests written using the Given/When/Then style popularized by cucumber. I'm using a framework which basically works the same as NBehave.

A recurring issue I'm facing is the issue of setup and wiring up all the application state necessary for the integration test. Most of my tests look something like this:

Given an empty system
  And a new NetworkServer
  And a new ServerDatabase
  And a new Eventlogger
  And a new Networkconnection
  And a new LoggingClient
When the client logs a new event
Then it should appear in the server database

As you can see the action and assertion are single lines, but I have 6 lines of 'wiring'. Almost every test I have repeats these 6 lines.

This seems like a code smell to me, but I'm unsure as to how to handle this. I could refactor the 6 lines into a single one (Given "a valid system..." or somesuch) but that seems like it goes too far and I'd be hiding too much information.

I'd appreciate any thoughts from others with more experience in this area. Thanks a lot.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

浅沫记忆 2024-07-30 07:16:48

对我来说,这听起来就像您希望有一个基类来执行设置之类的事情,然后让您的测试类从该基类继承,并且仅添加新的测试功能。

Base():
  constructor():
    do your wiring
    test if everything's ok


TestClass : Base
  constructor():
    Base.constructor()
    additional setup?

  test_functions()
    ..

To me this smells like you'd like to have a base class doing the setup kind of things and then have your test classes inherit from this base and only add the new testing functionality.

Base():
  constructor():
    do your wiring
    test if everything's ok


TestClass : Base
  constructor():
    Base.constructor()
    additional setup?

  test_functions()
    ..
野味少女 2024-07-30 07:16:48

我们有这样的东西

public abstract class ContextSpecification
{
    [FixtureSetUp]
    public void SetUp()
    {
        EstablishContext();
        Act();
    }

    protected abstract void Act();

    protected abstract void EstablishContext();

    [FixtureTearDown]
    public void TidyUpCore()
    {
        TidyUp();
    }

    protected virtual void TidyUp()
    {

    }
}

然后,对于每组类似的测试,我们创建一个像这样的 BaseContext:

internal class TestClassTests 
{
    internal abstract class BaseContext : ContextSpecification
    {
        protected TestClass _sut;

        protected override void Act()
        {

        }

        protected override void EstablishContext()
        {
            _sut = new TestClass ();
           // common wiring
        }
    }

   internal class Given_this_situation : BaseContext
   {
       protected override void EstablishContext()
       {
           base.EstablishContext();
           // test specific wiring
       }

       protected override void Act()
       {
           // carry out the test actions
       }

       [UnitTest]
       public void ThisShouldBeTrue()
       {
          Assert.IsTrue();
       }
   }
}

We have something like this

public abstract class ContextSpecification
{
    [FixtureSetUp]
    public void SetUp()
    {
        EstablishContext();
        Act();
    }

    protected abstract void Act();

    protected abstract void EstablishContext();

    [FixtureTearDown]
    public void TidyUpCore()
    {
        TidyUp();
    }

    protected virtual void TidyUp()
    {

    }
}

Then for each group of similar tests we create a BaseContext like this:

internal class TestClassTests 
{
    internal abstract class BaseContext : ContextSpecification
    {
        protected TestClass _sut;

        protected override void Act()
        {

        }

        protected override void EstablishContext()
        {
            _sut = new TestClass ();
           // common wiring
        }
    }

   internal class Given_this_situation : BaseContext
   {
       protected override void EstablishContext()
       {
           base.EstablishContext();
           // test specific wiring
       }

       protected override void Act()
       {
           // carry out the test actions
       }

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