Test Case Automation Code framework

Source: Internet
Author: User

This is my experience in automating the case writing framework, as the author's experience is limited, and if there are errors, please
In addition I have the code example, but do not know how to upload, as if not supporting the attachment Ah, I upload to the resources, can not find the classmate may leave a message, I sent you privately
Frame background

In a test project, there are many versions of the project, requiring testers to test each version as thoroughly as possible. Automated case can greatly improve the efficiency of testing, reduce the possibility of human error, and benefit the quality assurance of the project. With the expansion of project size and the increase of project case, the test case is written to the testers, this article is to solve this problem.

Framework Benefits
    1. Simple
    2. Reusing code
    3. Hierarchy of
    4. Easy to expand
    5. Enable testers to focus on the writing of business logic to improve the efficiency of automated case writing
    6. Easy maintenance of code
Framework uses

For the use of frames, from a small case, in the author test of a project, often testing the network connectivity case, the traditional case, may require more than 10 or even dozens of lines of code, and the use of automated case writing framework, only a simple 3 lines of statements, can do a good job of this task

The code is as follows

#-*-Coding:utf-8-*-
__author__ = ' Ligengyong '

‘‘‘
A sample test for auto case framework
‘‘‘

Import Casebase

class case (casebase.casebase):
    def __init__ (self):
        & nbsp casebase.casebase.__init__ (Self,  "host ping itself" ) #case name

def casework (self):
cmd = "Ping-c 3 127.0.0.1"
Flag = self.verifysumlocal (cmd, "icmp_seq", 3)
Self.organiseoutputdefault (flag)

def work (self):
Self.casework ()

if __name__ = = "__main__":
case = case ()
Case.start ()

To view the execution results:

[[email protected]bb-nsi-uinp09.bb01.baidu.com ligengyong]# python demo.py 
/usr/local/lib/python2.7/site-packages/crypto/util/number.py:57:powminsecurewarning:not using Mpz_powm_sec. You should rebuild using LIBGMP >= 5 to avoid timing attack vulnerability.
_warn ("not using MPZ_POWM_SEC. You should rebuild using LIBGMP >= 5 to avoid timing attack vulnerability. ", powminsecurewarning)
exe cmd at local h Ost:ping-c 3 127.0.0.1   #执行命令
Return:

PING 127.0.0.1 (127.0.0.1) bytes of data. #返回结果

Bytes from 127.0.0.1:icmp_seq=1 ttl=64 time=0.014 ms

Bytes from 127.0.0.1:icmp_seq=2 ttl=64 time=0.031 ms

Bytes from 127.0.0.1:icmp_seq=3 ttl=64 time=0.018 ms

---127.0.0.1 ping statistics---

3 Packets transmitted, 3 received, 0% packet loss, time 1999ms

RTT Min/avg/max/mdev = 0.014/0.021/0.031/0.007 ms

#case执行情况

Project:project
version:1.0.0
Case:host Ping itself
start_time:12:19:56 07/04/2015
end_time:12:19:58 07/04/2015
Expectation:success
Result:success
Reason:success

Framework Introduction Framework class diagram

about a few classes in the diagram about the

      case A, B, C for the specific case class, the cases type and casec in the demo example in this article are similar to the
    1. casebase Automation Code Foundation Framework class, Inherited the Threading.thread class, implemented the Run method, run called three methods,
      A. Call the work method, this method executes the task, the subclass implements
      B. Call the timestamp function, and then calculate the update time after the case execution ends
      C. Call case to run the result output function, and the result of the statistics case execution
    2. Projectbase inherits Casebase, which is used to write the configuration and common functions of some project planes, and to standardize the execution of cases
      for example, The author of the project, often to perform some commands on machine A, Casebase provides the function exeremote (self, cmd, IP, port, name, password, timeout=10) but I do not want to enter the IP of host a every time, Password and so on, so I write a
      Execmdathosta () method, in Execmdathosta in accordance with Projectbase for host a configuration information on the Exeremote method for further encapsulation, So all the subclass case can simply call this method can be, both simplify the code, and easy to understand, while the configuration information of host a changes or the function of the Execmdathosta method needs to be modified, only need to change one place, which makes the code easy to maintain
      Another situation, if the case requires a pre-collation environment, perform tasks, and then clean up the environment, so that you can rewrite the work method, the new sub-work method, in the work method call the collation environment, execute the sub-work method, clean up the environment, let the subclass to rewrite the child work method The
    3. Modulebase inherits Projectbase, which has a similar function to Projectbase, but is a module-level message

Through a simple introduction, it is not difficult to see the framework has the following characteristics

    1. Dependency inversion, which makes the program dependent on abstract classes rather than specific classes
    2. Hierarchical multiplexing Code
    3. Closed for modification, open for expansion
    4. The start of the program is done by the framework, and the user is responsible for the specific business logic
    5. Good extensibility, users can expand or modify the implementation process of the framework
Casebase Introduction

In the previous section, Have seen Casebase inherited the Threading.thread class, the following casebase other methods to do a brief introduction, due to the author's level of limited and participate in the test project, may not be all the testing of the basic methods are encapsulated in, follow-up if there are other requirements, can be added in

Let's start by introducing the public method:

    1. Closeprocesslike (ProcessName), based on the name of the process, is found locally and killed
    2. Closeprocesslikeremote (ProcessName, IP, port, name, password, timeout=10) is found in the target host according to the process name, and kill, if it fails, throws an exception

Next introduce the class method of Casebase

  1. __init__ (self, Name) class initialization method, name of case
  2. Initparameter (self) Initialize variable
  3. Setprojectandversion (Self, name= "project", version= "1.0.0") sets the name and version of the project, which is generally covered in projectbase
  4. Def work specific execution method, subclass override required
  5. Setoutputfile (self, name) sets output file, if not set, outputs to standard output
  6. Puts (self, line) custom output method, if subclasses need to print something, you can call this method
  7. Touch (self, time) to update the timestamp
  8. Done to output the execution of a case after the scenario execution
  9. Run (self) overrides threading. Thread's Run method
  10. Getresultstring (self) generates execution result string
  11. Verifyhaslocal (self, cmd, target) executes CMD on the machine and see if the returned result contains a target if there is a return of true if there is no return false
  12. Sleep (self, timeout) custom sleep method
  13. Verifysumlocal (self, cmd, target, sum) executes CMD on the machine and looks at whether the returned result contains the number of target if it is sum, if it is true, if it returns false
  14. Exelocal (self, cmd) executes command cmd locally
  15. Exeremote (self, cmd, IP, port, name, password, timeout=10) Execute command cmd remotely
  16. Getretfromexeremote (self, cmd, IP, port, name, password, timeout=10) executes command cmd remotely and returns execution results to provide an interface for user-defined judgments
  17. Transportfile (self, file, RemoteFile, IP, name, password, sshport=22) send files to the far end
  18. Verifyhasremote (self, cmd, target, IP, port, name, password, timeout=1) is similar to verifyhaslocal, except that it is executed at the far end.
  19. The Verifysumremote (self, cmd, target, SUM, IP, port, name, password, timeout=10) and verifysumlocal functions are similar, except for the remote execution
  20. Organiseoutputdefault (self, flag) The output of the default organization case execution result, flag for the executed identity, true for success, and false for failure

TestException the custom exception class

Other matters:

    1. If a case requires two different threads, it is necessary to rewrite the done method of a thread without any processing, so as to avoid having two threads simultaneously outputting a case execution node

      def done (self):
      Pass

2. If a case requires two identical threads, call, Setoutputflag (False) to


Test Case Automation Code framework

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.