您的测试中有哪些最佳实践?

时间:2022-10-16 00:04:37

I would like to know what are your practices when you test your classes.

我想知道在测试课程时你的做法是什么。

For example, I like to use inheritance, with my fixtures. Given two classes BaseClass, SubClass, I make two other classes BaseClassFixture and SubClassFixture (SubClassFixture is a sub class of BaseClassFixture). So I'm sure that I don't break code which use SubClass as a BaseClass (And people who extends my class can be sure if they do things right, by creating another sub class of my fixture).

例如,我喜欢使用继承,使用我的灯具。给定两个类BaseClass,SubClass,我创建另外两个类BaseClassFixture和SubClassFixture(SubClassFixture是BaseClassFixture的子类)。所以我确信我不会破坏使用SubClass作为BaseClass的代码(扩展我的类的人可以确定他们是否做得对,通过创建我的fixture的另一个子类)。

I do fixture inheritance with interfaces too. For example, when I create a fixture for IList, I check that any Add, increase Count by one. When I have a concrete class which implements IList I just create a fixture named MyConcreteClassIListFixture.

我也用接口做夹具继承。例如,当我为IList创建一个fixture时,我会检查是否有任何Add,将Count增加一个。当我有一个实现IList的具体类时,我只需创建一个名为MyConcreteClassIListFixture的夹具。

In that case, the fixture for my interface is abstract and I let my subclass create the instance for my tests.

在这种情况下,我的界面的fixture是抽象的,我让我的子类为我的测试创建实例。

I think it's a kind of design by contracts (see Bertrand Meyer), because I check invariant before and after any tests.

我认为这是一种合同设计(参见Bertrand Meyer),因为我在任何测试之前和之后检查不变量。

I do this especially with published interfaces or classes.

我这样做尤其是使用已发布的接口或类。

And you... what are your practices ??

而你......你的做法是什么?

2 个解决方案

#1


0  

There are couple of important things when writing unit test.

编写单元测试时有几个重要的事情。

1) Unit Tests should be independent:

1)单元测试应该是独立的:

Unit Tests must be independent. This means that your unit test should not depend on external things to run. This includes internet connection, external web services etc.

单元测试必须是独立的。这意味着您的单元测试不应该依赖于外部事物来运行。这包括互联网连接,外部网络服务等。

2) Unit Tests should be fast:

2)单元测试应该很快:

Unit Tests should run fast. You can write unit tests in multiple ways. Some of them include the dataaccess even though you don't need to access data to run the test. You can always use mock objects and mock the data access layer.

单元测试应该快速运行。您可以通过多种方式编写单元测试。其中一些包括dataaccess,即使您不需要访问数据来运行测试。您始终可以使用模拟对象并模拟数据访问层。

3) Good naming convention:

3)良好的命名约定:

Unit Tests should have good naming convention and should read like stories.

单元测试应具有良好的命名约定,应该像故事一样阅读。

Here is one example:

这是一个例子:

public class when_user_transfer_money_from_source_account_to_destination_account

公共类when_user_transfer_money_from_source_account_to_destination_account

public void make_sure_error_is_thrown_when_source_account_has_insufficient_funds() {

public void make_sure_error_is_thrown_when_source_account_has_insufficient_funds(){

}

Here is a good screencast that covers many of the above points:

这是一个很好的截屏视频,涵盖了以上几点:

http://screencastaday.com/ScreenCasts/32_Introduction_to_Mocking.aspx

#2


1  

My most important rule is that each test should be atomic and should run in no particular order.

我最重要的规则是每个测试都应该是原子的,并且应该没有特定的顺序运行。

For unit tests, they should strictly obey seperation of concerns. For integration tests, I pay extra attention to make sure they follow the most important rule.

对于单元测试,他们应该严格遵守关注点。对于集成测试,我要特别注意确保它们遵循最重要的规则。

Also, tests should follow the DRY rule as much as possible along with the code.

此外,测试应尽可能遵循DRY规则以及代码。

#1


0  

There are couple of important things when writing unit test.

编写单元测试时有几个重要的事情。

1) Unit Tests should be independent:

1)单元测试应该是独立的:

Unit Tests must be independent. This means that your unit test should not depend on external things to run. This includes internet connection, external web services etc.

单元测试必须是独立的。这意味着您的单元测试不应该依赖于外部事物来运行。这包括互联网连接,外部网络服务等。

2) Unit Tests should be fast:

2)单元测试应该很快:

Unit Tests should run fast. You can write unit tests in multiple ways. Some of them include the dataaccess even though you don't need to access data to run the test. You can always use mock objects and mock the data access layer.

单元测试应该快速运行。您可以通过多种方式编写单元测试。其中一些包括dataaccess,即使您不需要访问数据来运行测试。您始终可以使用模拟对象并模拟数据访问层。

3) Good naming convention:

3)良好的命名约定:

Unit Tests should have good naming convention and should read like stories.

单元测试应具有良好的命名约定,应该像故事一样阅读。

Here is one example:

这是一个例子:

public class when_user_transfer_money_from_source_account_to_destination_account

公共类when_user_transfer_money_from_source_account_to_destination_account

public void make_sure_error_is_thrown_when_source_account_has_insufficient_funds() {

public void make_sure_error_is_thrown_when_source_account_has_insufficient_funds(){

}

Here is a good screencast that covers many of the above points:

这是一个很好的截屏视频,涵盖了以上几点:

http://screencastaday.com/ScreenCasts/32_Introduction_to_Mocking.aspx

#2


1  

My most important rule is that each test should be atomic and should run in no particular order.

我最重要的规则是每个测试都应该是原子的,并且应该没有特定的顺序运行。

For unit tests, they should strictly obey seperation of concerns. For integration tests, I pay extra attention to make sure they follow the most important rule.

对于单元测试,他们应该严格遵守关注点。对于集成测试,我要特别注意确保它们遵循最重要的规则。

Also, tests should follow the DRY rule as much as possible along with the code.

此外,测试应尽可能遵循DRY规则以及代码。