简体   繁体   English

正确的单元测试理念

[英]Proper Unit Testing Philosophy

What would be the proper thing to do for each case? 在每种情况下,正确的做法是什么?

1: Context: Testing a function that creates a database as well as generating metadata for that database 1:上下文:测试创建数据库以及为该数据库生成元数据的功能

Question: Normally unit test cases are supposed to be independent, but if we want to make sure the function raises an exception when trying to make a duplicate database, would it be acceptable to have ordered test cases where the first one tests if the function works, and the second one tests if it fails when calling it again? 问题:通常,单元测试用例应该是独立的,但是如果我们要确保在尝试创建重复数据库时该函数引发异常,那么在第一个测试该函数是否工作的情况下,订购有序的测试用例是否可以接受? ,第二个测试再次调用它是否失败?

2: Most of the other functions require a database and metadata. 2:其他大多数功能都需要数据库和元数据。 Would it be better to call the previous functions in the set up of each test suite to create the database and metadata, or would it be better to hard code the required information in the database? 在每个测试套件的设置中调用以前的功能来创建数据库和元数据会更好,还是在数据库中对所需信息进行硬编码会更好?

Your automated test should model the following: 您的自动化测试应为以下模型建模:

  1. Setup 设定
  2. Exercise (SUT) 运动(SUT)
  3. Verify 校验
  4. Teardown 拆除

In addition, each test should be as concise as possible and only expose the details that are being tested. 另外,每个测试都应尽可能简洁,并且仅公开要测试的细节。 All other infrastructure that is required to execute the test should be abstracted away so that the test method serves as documention that only exposes the inputs that are being tested in regards to what you want to verify for that particular test. 应该抽象出执行测试所需的所有其他基础结构,以便测试方法充当文档,仅针对要针对该特定测试验证的内容公开正在测试的输入。

Each test should strive to start from a clean slate so that the test can be repeated with the same results each time regardless of the results of prior tests that have been executed. 每个测试都应努力从一个干净的表盘开始,以便每次执行时都可以以相同的结果重复该测试,而不管先前执行的测试的结果如何。

I typically execute a test-setup and a test-cleanup method for each integration test or any test that depends on singletons that maintain state for the System-Under-Test and need to have it's state wiped. 我通常为每个集成测试或任何依赖单例的测试执行一个测试设置和一个测试清除方法,这些测试保持系统处于测试中的状态并需要擦除其状态。

Normally unit test cases are supposed to be independent, but if we want to make sure the function raises an exception when trying to make a duplicate database, would it be acceptable to have ordered test cases where the first one tests if the function works, and the second one tests if it fails when calling it again? 通常,单元测试用例应该是独立的,但是如果我们要确保在尝试创建重复数据库时该函数引发异常,那么在第一个测试该函数是否工作的情况下,对有序测试用例进行排序是否可以接受,并且第二个测试再次调用它时是否失败?

No, ordered tests are bad. 不,有序测试是不好的。 There's nothing stopping you from having a test call another method that happens to be a test though: 但是,没有什么可以阻止您进行测试调用恰巧是测试的另一个方法:

@Test
public void createDataBase(){
  ...
}

@Test
public void creatingDuplicateDatabaseShouldFail(){
   createDataBase();
   try{
      //call create again should fail
      //could also use ExpectedException Rule here
      createDataBase();
      fail(...);
   }catch(...){ 
    ...
   }
}

Most of the other functions require a database and metadata. 其他大多数功能都需要数据库和元数据。 Would it be better to call the previous functions in the set up of each test suite to create the database and metadata, or would it be better to hard code the required information in the database? 在每个测试套件的设置中调用以前的功能来创建数据库和元数据会更好,还是在数据库中对所需信息进行硬编码会更好?

If you use a database testing framework like DbUnit or something similar, it can reuse the same db setup over and over again in each test. 如果您使用数据库测试框架(例如DbUnit或类似的东西),则它可以在每次测试中反复使用相同的数据库设置。

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

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