简体   繁体   English

在Linux上运行的C ++程序中为类编写测试用例

[英]writing testcases for classes in my c++ program running on linux

I am a beginner for writing test-cases so i don't know about test-cases, can you briefly explain about test-cases and also, how can i write test-cases for my c++ program(classes,member functions,member variables,....) help me in writing those and compiling and running it. 我是编写测试用例的初学者,所以我不了解测试用例,您能否简要说明一下测试用例,以及如何为我的C ++程序编写测试用例(类,成员函数,成员变量,....)帮助我编写这些文件并进行编译和运行。

Advance in thanks 提前感谢

First of all: 首先:

  1. First find your self a framework of your liking: wikipedia list of frameworks here 首先找到自己喜欢的框架Wikipedia框架清单
  2. Install and read its documentation 安装并阅读其文档
  3. Identify the invariants, valid and invalid input of your methods and write tests that makes sure that they are enforced. 识别方法的不变式,有效和无效输入,并编写测试以确保它们得到强制执行。 My recommendation is to write tests that test in a black box way. 我的建议是编写以黑盒方式进行测试的测试。 Ie you do not care how the methods does it, but you check that the results are ok with good input (corner cases and a normal case) and also that it fails in a good way when the input is bad. 也就是说,您不在乎这些方法是如何做到的,但是您可以在输入正确的情况下检查结果是否正确(角落情况和正常情况),并且在输入错误时以良好的方式失败。
  4. This answer elaborates on how to write good tests 这个答案详细说明了如何编写好的测试

Some general advice: 一些一般建议:

  • Write tests that will either fail or not, writing tests that probably will fail when something is wrong is a pita. 编写可能会失败或不会失败的测试,写出可能出故障的测试是皮塔饼。
  • Never trust a test that you haven't seen fail. 永远不要相信没有失败的测试。
  • Write one test at the time 当时写一个测试
  • Try to keep the testcases as autonomous as possible. 尽量保持测试用例的自治。 Make sure that you test the code submitted to testing, not it's infrastructure. 确保测试提交给测试的代码,而不是基础结构。 Unit testing is a great way to make sure that your classes depends on interfaces. 单元测试是确保您的类依赖于接口的好方法。
  • It is most often a bad idea to test implementation details of a method. 测试方法的实现细节通常是一个坏主意。 You do not want tests to fail when someone rewrites the code correctly. 当有人正确重写代码时,您不希望测试失败。 If you feel the need to test the implementation you probably have another class hiding in the code that also should be unittested. 如果您需要测试实现,则可能在代码中隐藏了另一个类,该类也应该进行单元测试。

Cxxtest确实有用。

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

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