简体   繁体   English

逐步的功能测试自动化

[英]Step-by-step functional testing automation

I have a basic class in C# from which I create inherited classes for databinding scenarios. 我在C#中有一个基本类,从中可以为数据绑定方案创建继承的类。 You can think of it as a substitute for .NET's DataRow class. 您可以将其视为.NET的DataRow类的替代品。

I want to automate testing of a typical row's lifetime, making sure that things such as object state and changes detection remains coherent throughout. 我想自动测试典型行的生命周期,以确保对象状态和更改检测之类的内容始终保持一致。

My first thought was to simply use unit test class with a method that would do multiple operations and consequent assertions, like this: 我的第一个想法是简单地将单元测试类与可以执行多种操作和随后声明的方法一起使用,如下所示:

/*
For the sake of keeping this as simple as possible, let's just assume
that new instances can be created with a public constructor, as "unchanged".
*/

var row = new PocoTest(); // Derived from aforementionned base class

Assert.AreEqual(RecordStates.Unchanged, row.RecordState);
Assert.IsFalse(row.IsPropertyModified("MyProperty"));

row.MyProperty = "this is a new value";

Assert.AreEqual(RecordStates.Modified, row.RecordState);
Assert.IsTrue(row.IsPropertyModified("MyProperty"));

row.AcceptChanges();

Assert.AreEqual(RecordStates.Unchanged, row.RecordState);
Assert.IsFalse(row.IsPropertyModified("MyProperty"));

However, this doesn't feel right in the unit testing paradigm in which it is recommended to have only one thing at a time being tested. 但是,这在单元测试范式中感觉不对,在这种情况下,建议一次只测试一件事。

So I'm kind of looking for some advice, here. 所以,我在这里寻求一些建议。 Am I overthinking this? 我在想这个吗? Should I just keep doing it this way? 我应该继续这样做吗? Or is there another, better and more adapted way to accomplish something like this? 还是有另一种更好,更适应的方式来完成这样的事情?

your tests should each be testing one logical thing. 您的测试应分别测试一种合乎逻辑的事物。 This may require several steps or assertions to verify. 这可能需要几个步骤或断言来进行验证。 This is fine. 这可以。

However your tests look to me like 3 tests. 但是,您的测试在我看来就像3个测试。 1 which verifies that upon creation the property is unchanged, and then one which verifies that when its changed the object reflects that and then one which verifies that after accepting changes the state is unchanged again. 1验证创建后该属性未更改,然后一个验证其更改后的对象反映了该属性,然后一个在接受更改后验证状态仍未更改的对象。

What have you called your test currently. 您目前如何称呼您的考试。 I suspect that you have had trouble giving it a name, or it has a long convoluted name. 我怀疑您在给它起一个名字时遇到麻烦,或者它的名字太长了。 Splitting it it should give you nice descriptive names. 拆分它应该为您提供漂亮的描述性名称。

I'd go with this: 我会这样:

public void WhenRowIsCreatedItShouldBeInAnUnchangedState()
{
  var row = new PocoTest(); // Derived from aforementionned base class

  Assert.AreEqual(RecordStates.Unchanged, row.RecordState);
  Assert.IsFalse(row.IsPropertyModified("MyProperty"));
}


public void WhenPropertyIsChangedItShouldBeInAModifiedState()
{
  var row = new PocoTest(); // Derived from aforementionned base class
  row.MyProperty = "this is a new value";

  Assert.AreEqual(RecordStates.Modified, row.RecordState);
  Assert.IsTrue(row.IsPropertyModified("MyProperty"));
}

public void WhenChangesAreAcceptedItShouldBeInAnUnchangedState()
{
  var row = new PocoTest(); // Derived from aforementionned base class
  row.MyProperty = "this is a new value";
  row.AcceptChanges();

  Assert.AreEqual(RecordStates.Unchanged, row.RecordState);
  Assert.IsFalse(row.IsPropertyModified("MyProperty"));
}

I'll have to be controversial here - I think the "one test, one test method" is a dogma rather than a useful tool. 我在这里必须引起争议-我认为“一种测试,一种测试方法”是一种教条,而不是一种有用的工具。 Its a matter of whether your test is whole, complete and predictable. 这取决于您的测试是否完整,完整和可预测。 I'd say go with your current approach - after all what is to be gained by splitting it up and adding complexity. 我要说说您当前的方法-毕竟,将其拆分并增加复杂性将获得什么。 Your code currently has lower cyclomatic complexity than adding in an extra function call for each Assert. 与为每个Assert添加额外的函数调用相比,您的代码当前的循环复杂度更低。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM