简体   繁体   English

单元测试。 关于独立测试

[英]Unit testing. About independent tests

Before downvote my question please listen :) I use unit test. 在降级我的问题之前,请听:)我使用单元测试。 And I know they must be independently from each other. 而且我知道他们必须彼此独立。 Though how to test repository for book. 虽然如何测试书库。 First book must be created, then updated and then deleted. 必须先创建第一本书,然后进行更新然后删除。 Three unit tests I have. 我有三个单元测试。 Isn't it right to have dependency for these unit tests? 依赖这些单元测试不是对的吗?

   [TestFixture]
    public class CompanyRepositoryTests
    {
        public void CreateCompany()
        {
           // This must be called 1
        }

        public void UpdateCompany()
        {
           // This must be called 2
        }

        public void DeleteCompany()
        {
            // This must be called 3
        }
    }

A thing about unit tests (as opposed to integration tests) is that they're only supposed to test a single method. 关于单元测试(与集成测试相对)的一件事是,它们仅应测试一种方法。 So for instance, if you have a method that looks like this: 因此,例如,如果您有一个如下所示的方法:

public void createCompany(Company c) {
  if (c == null) {
    throw new NullPointerException();
  }
  database.create(c);
}

A good test for it will only test two things: 一个好的测试只能测试两件事:

  1. If null is passed in, NullPointerException is thrown 如果传入null ,则抛出NullPointerException
  2. If non- null is passed in, database.create() gets called 如果传入非null则调用database.create()

Whether database.create() actually does anything useful is a different question. database.create()是否真正有用吗是另一个问题。 One that has to do with unit-testing the database. 这与对数据库进行单元测试有关。

In your case, mock out the underlying storage as suggested, and write proper unit tests. 在您的情况下,请根据建议模拟基础存储,并编写适当的单元测试。

In integration tests on the other hand, having dependencies is clearly fine. 另一方面,在集成测试中,具有依赖关系显然很好。 Even if so, your tests should look like 即使这样,您的测试也应该看起来像

  • test 1: fresh database: create a company, make sure it's there. 测试1:全新的数据库:创建公司,并确保其存在。
  • test 2: fresh database: create a company, delete it, make sure it's gone. 测试2:全新的数据库:创建一个公司,将其删除,确保它不存在。
  • test 3: fresh database: create a company, modify it, make sure it worked. 测试3:新数据库:创建公司,对其进行修改,确保其正常工作。

every test should be starting from scratch. 每个测试都应该从头开始。

In this exercise, you will learn how to test CRUD actions that modify the database. 在本练习中,您将学习如何测试修改数据库的CRUD操作。 One of the premises to take into account is that tests don't necessarily run in a specific order. 要考虑的前提之一是测试不一定按特定顺序运行。 Another one is that every test could be executed as many times as needed. 另一个是,每个测试可以根据需要执行多次。 So your tests should not depend on previous tests executions. 因此,您的测试不应依赖于先前的测试执行。 For instance, if your test is going to delete a record in the database, the setup of the test should provide the record to be deleted. 例如,如果您的测试要删除数据库中的记录,则测试的设置应提供要删除的记录。 This way, the test could be executed repeatedly. 这样,可以重复执行测试。

Good tutorial about unit tests 关于单元测试的好教程

you could mock the dependencies. 您可以模拟依赖项。

hava a look at http://code.google.com/p/mockito/ 看看http://code.google.com/p/mockito/

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

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