简体   繁体   English

如何编写自动化测试-将测试用例作为函数或将测试用例作为类

[英]How to write automated tests - Test case as a function or test case as a class

I am having a design problem in test automation:- 我在测试自动化中遇到设计问题:-

Requirements - Need to test different servers (using unix console and not GUI) through automation framework. 要求-需要通过自动化框架测试不同的服务器(使用UNIX控制台而不是GUI)。 Tests which I'm going to run - Unit, System, Integration 我要运行的测试-单元,系统,集成

Question: While designing a test case, I am thinking that a Test Case should be a part of a test suite (test suite is a class), just as we have in Python's pyunit framework. 问:在设计测试用例时,我认为测试用例应该成为测试套件的一部分(测试套件是一个类),就像在Python的pyunit框架中一样。 But, should we keep test cases as functions for a scalable automation framework or should be keep test cases as separate classes(each having their own setup, run and teardown methods) ? 但是,我们应该将测试用例保留为可扩展的自动化框架的功能还是应该将测试用例保留为单独的类(每个类都有各自的设置,运行和拆卸方法)? From automation perspective, Is the idea of having a test case as a class more scalable, maintainable or as a function? 从自动化的角度来看,将测试用例作为一类的想法是否更具可扩展性,可维护性或功能性?

Normally Test Cases are used as classes rather than functions because each test case has own setup data and initialization mechanism. 通常,将测试用例用作类而不是函数,因为每个测试用例都有自己的设置数据和初始化机制。 Implementing test cases as a single function will not only make difficult to set up test data before running any test case, but yes you can have different test method in a test case class if you are running same test scenario. 将测试用例实现为单个功能不仅会使得在运行任何测试用例之前难以设置测试数据,而且是的,如果您运行的是相同的测试场景,则可以在测试用例类中使用不同的测试方法。

The following is my opinion: 以下是我的意见:

Pros of writing tests as functions: 将测试编写为函数的优点:

  • If you need any pre-requisites for that test case, just call another function which provides the pre-requisites. 如果您需要该测试用例的任何先决条件,则只需调用另一个提供先决条件的函数即可。 Do the same thing for teardown steps 对拆卸步骤执行相同的操作
  • Looks simple for a new person in the team. 对于团队中的新手来说看起来很简单。 Easy to undertstand what is happening by looking into tests as functions 通过将测试视为函数可以轻松理解正在发生的事情

Cons of writing tests as functions: 将测试编写为函数的缺点:

  • Not maintainable - Because if there are huge number of tests where same kind of pre-requisites are required, the test case author has to maintain calling each pre-requisite function in the test case. 不可维护-因为如果存在大量需要相同先决条件的测试,则测试用例作者必须维护调用测试用例中的每个必备功能。 Same for each teardown inside the test case 测试用例中的每个拆解都相同

  • If there are so many calls to such a pre-requisite function inside many test cases, and if anything changes in the product functionality etc, you have to manually make efforts in many places again. 如果在许多测试用例中有如此多的对此类必备功能的调用,并且产品功能等发生任何变化,则您必须在许多地方再次手动进行工作。

Pros of writing test cases as classes: 将测试用例编写为类的优点:

  • Setup, run and teardown are clearly defined. 设置,运行和拆卸均已明确定义。 the test pre-requisites are easily understood 测试先决条件很容易理解
  • If there is Test 1 which is does something and the result of Test 1 is used as a setup pre-requisite in Test 2 and 3, its easy to just inherit from Test 1, and call its setup, run a teardown methods first, and then, continue your tests. 如果存在要执行某项操作的Test 1,并且将Test 1的结果用作Test 2和3中的设置先决条件,则可以轻松地从Test 1继承它,并调用其设置,首先运行拆卸方法,然后然后,继续测试。 This helps make the tests independent of each other. 这有助于使测试彼此独立。 Here, you dont need to make efforts to maintain the actual calling of your code. 在这里,您不需要付出任何努力来维护代码的实际调用。 It will be done implicitly because of inheritance. 由于继承,它将隐式完成。
  • Sometimes, if the setup method of Test 1 and run method of Test 2 might become the pre-requisites of another Test 3. In that case, just inherit from both of Test 1 and Test 2 classes and in the Test 3's setup method, call the setup of Test 1 and run of Test 2. Again you dont need to need to maintain the calling of the actual code, because you are calling the setup and run methods, which are tried and tested from the framework perspective. 有时,如果Test 1的设置方法和Test 2的运行方法可能成为另一个Test 3的先决条件。在这种情况下,只需继承Test 1和Test 2类,并在Test 3的setup方法中调用测试1的设置和测试2的运行。再次,您不需要维护实际代码的调用,因为您正在调用setup和run方法,这些方法是从框架的角度进行尝试和测试的。

Cons of writing test case as classes: 将测试用例编写为类的缺点:

  • When the number of tests increase, you cant look into a particular test and say what it does, because it may have inherited so much levels that you cant back track. 当测试数量增加时,您将无法查看特定的测试并说出它的功能,因为它可能继承了太多的级别,您无法回溯。 But, there is a solution around it - Write doc strings in each setup, run, teardown method of each test case . 但是,有一个解决方案-在每个测试用例的每种设置,运行,拆卸方法中编写文档字符串 And, write a custom wrapper to generate doc strings for each test case. 并且,编写一个自定义包装器以为每个测试用例生成文档字符串。 While/After inheriting, you should provide an option to add/Remove the docstring of a particular function (setup, run, teardown) to the inherited function. 在继承过程中/之后,您应该提供一个选项,以将特定函数的文档字符串(设置,运行,拆卸)添加/删除到继承的函数中。 This way, you can just run that wrapper and get information about a test case from its doc-strings 这样,您可以运行该包装器并从其文档字符串获取有关测试用例的信息

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

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