简体   繁体   English

如何编写测试用例?

[英]How to write Test Cases?

I want to learn how to write test cases before writing the code. 我想学习如何在编写代码之前编写测试用例。 I read an article about test-driven development. 我读了一篇关于测试驱动开发的文章。 I wonder how developers write test cases? 我想知道开发人员如何编写测试用例 For Example this method: 例如这个方法:

    public int divideNumbers(int num1, int num2)
    {
      return num1 / num2;
    }

We start with a blank project now. 我们现在开始一个空白项目。 You want to do something, say divide two numbers. 你想做点什么,比如分两个数字。 So you write a test describing what you want to do: 所以你写了一个描述你想做什么的测试:

Assert.That(divide(10,2), Eq(5))

This test gives you an entry point: it describes the acceptable interface of the divide method. 此测试为您提供了一个入口点:它描述了divide方法的可接受接口。 So you proceed to implement it as int divide(int x, int y) for example. 因此,您可以继续将其实现为int divide(int x, int y)

Write tests that describe what you expect to get from your code. 编写测试,描述您希望从代码中获得的内容。 You don't need to think much about it. 你不需要考虑太多。 The most normal way of writing your expectation is probably the best way to design your code, and then you can implement it to satisfy your test. 编写期望的最常用方式可能是设计代码的最佳方式,然后您可以实现它以满足您的测试。

There are a few steps for testing. 有几个步骤可供测试。 From MSDN ; 来自MSDN ;

In your case; 在你的情况下;

Assert.AreEqual(divideNumbers(8, 4), 2);

Assert class verifies conditions in unit tests using true / false propositions. Assert类使用true / false命题验证单元测试中的条件。 You should write your test cases what you expecting their results. 您应该将测试用例写入您期望的结果。 You can use TestMethod attribute for your test methods. 您可以将TestMethod属性用于测试方法。 There is a cool post about Creating Unit tests for your c# code . 关于为c#代码创建单元测试有一篇很酷的帖子。 Analyze it very well. 分析得很好。

Start with a stub of the function/class/component that you want to develop. 从您要开发的函数/类/组件的存根开始。 It should compile, but deliberately not (yet) do what it is supposed to do. 它应该编译,但故意不(还)做它应该做的事情。

For example: 例如:

public int divideNumbers(int num1, int num2)
{
    throw new NotImplementedException();
}

or 要么

    return -42;

Think about the intended behavior, treating the stub as an interface to a black box. 考虑预期的行为,将存根视为黑盒子的接口。 Don't care about the implementation (yet). 不关心实现(还)。 Think about the "contract" of the interface: X goes in, Y goes out. 想想界面的“契约”:X进去,Y出去。

Identify standard cases and important egde cases. 确定标准案例和重要案件。 Write tests for them. 为他们写测试。

For integer division (assuming we would write it from scratch) there are actually quite a couple of cases to consider: With and without remainder, n/1, n/0, 0/n, 0/0, negative numbers, etc. 对于整数除法(假设我们将从头开始编写),实际上有几种情况需要考虑:有和没有余数,n / 1,n / 0,0 / n,0/0,负数等。

Assert.IsTrue(divideNumbers(4,4) == 1);
Assert.IsTrue(divideNumbers(4,3) == 1);
Assert.IsTrue(divideNumbers(4,2) == 2);
Assert.IsTrue(divideNumbers(4,1) == 4);
Assert.Throws<ArgumentException>(() => divideNumbers(4,0));

Assert.IsTrue(divideNumbers(0,4) == 0);
Assert.Throws<ArgumentException>(() => divideNumbers(0,0));

Assert.IsTrue(divideNumbers( 4,-2) == -2);
Assert.IsTrue(divideNumbers(-4, 2) == -2);
Assert.IsTrue(divideNumbers(-4,-2) ==  2);

Assert.IsTrue(divideNumbers( 4,-3) == -1);
Assert.IsTrue(divideNumbers(-4, 3) == -1);
Assert.IsTrue(divideNumbers(-4,-3) ==  1);

Compile and run the unit tests. 编译并运行单元测试。 Do they all fail? 他们都失败了吗? If not, why? 如果没有,为什么? Maybe one of the tests is not working as intended (tests can be buggy, too!). 也许其中一个测试没有按预期工作(测试也可能是错误的!)。

Now, start to implement until no test fails anymore. 现在,开始实施,直到没有测试失败。

Start by realizing the difference between theory and practice. 首先要意识到理论与实践之间的区别。

Any real life system will have stuff that is easily created via TDD and some that are not. 任何现实生活系统都会有通过TDD轻松创建的东西,而有些则不是。

The last group is everything dependent on environment, when working on a system that does not seek to abstract away environmental assumptions, but pragmatically accept these. 最后一组是依赖于环境的一切,当在一个不寻求抽象出环境假设的系统上工作时,但是实际上接受这些假设。

This group can be developed in a TDD fashion, but it will require additional tooling and extensions to the software factory. 该组可以以TDD方式开发,但它需要额外的工具和软件工厂的扩展。

For .Net this would be tooling and extensions such as MS virtual Test Lab and SpecFlow. 对于.Net,这将是工具和扩展,如MS虚拟测试实验室和SpecFlow。

What I am trying to communicate is that it depends . 我想要传达的是它取决于它

For very simple component/unit testing, the idea would be to write a failing testcase, before writing the code to be tested, and ending development when test runs successfully. 对于非常简单的组件/单元测试,想法是在编写要测试的代码之前编写失败的测试用例,并在测试成功运行时结束开发。

For integration testing and beyond (system testing), you will need to consider, among other things, how to bring the test environment into some known state in addition to considering what to test for. 对于集成测试及其他(系统测试),除了考虑要测试的内容之外,还需要考虑如何将测试环境置于某种已知状态。

暂无
暂无

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

相关问题 如何在不违反单一责任原则的情况下编写类似的测试用例? - How to write similar test cases without breaking the Single Responsibility Principle? 如何用“out”参数编写私有函数的C#测试用例? - How to write C# test cases for private functions with “out” parameters? 如何在一种方法上用不同的情况编写断言测试,并且在与不同的响应案例进行比较时,所有这些都通过了? - How to write Assert test with different cases on one method and all of them pass when comparing with different response cases? 在VS2012中编写单元测试用例 - Write unit test cases in VS2012 如何在 c# 中为以下代码编写 nunit 测试用例? 如何为没有 arguments 的方法编写 nunit 测试用例? - How to write nunit test case for below code in c#? How ro write nunit test cases for methods with no arguments? 如何为 ASP.NET 中的代码隐藏文件中的方法编写 MSUnit/测试用例 - How to Write MSUnit /Test cases for methods in code behind files in ASP.NET 如何为 Program.cs 代码编写测试用例,没有主方法或内部没有任何方法? - How to write test cases for a Program.cs code, without main method or no any method inside? C#:如何编写构造函数和构造函数重载的测试用例? - C#: How do you write test cases for constructors and constructor overloads? 编写MsUnit和N单元测试用例的正确方法是什么 - What is the correct way to write MsUnit and N unit Test Cases 如何在 nunit 测试用例中使用 Author 属性 - how to use Author attribute in nunit test cases
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM