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
- Simple
- Reusing code
- Hierarchy of
- Easy to expand
- Enable testers to focus on the writing of business logic to improve the efficiency of automated case writing
- 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
- 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
- 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
- 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
- Dependency inversion, which makes the program dependent on abstract classes rather than specific classes
- Hierarchical multiplexing Code
- Closed for modification, open for expansion
- The start of the program is done by the framework, and the user is responsible for the specific business logic
- 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:
- Closeprocesslike (ProcessName), based on the name of the process, is found locally and killed
- 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
- __init__ (self, Name) class initialization method, name of case
- Initparameter (self) Initialize variable
- Setprojectandversion (Self, name= "project", version= "1.0.0") sets the name and version of the project, which is generally covered in projectbase
- Def work specific execution method, subclass override required
- Setoutputfile (self, name) sets output file, if not set, outputs to standard output
- Puts (self, line) custom output method, if subclasses need to print something, you can call this method
- Touch (self, time) to update the timestamp
- Done to output the execution of a case after the scenario execution
- Run (self) overrides threading. Thread's Run method
- Getresultstring (self) generates execution result string
- 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
- Sleep (self, timeout) custom sleep method
- 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
- Exelocal (self, cmd) executes command cmd locally
- Exeremote (self, cmd, IP, port, name, password, timeout=10) Execute command cmd remotely
- 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
- Transportfile (self, file, RemoteFile, IP, name, password, sshport=22) send files to the far end
- Verifyhasremote (self, cmd, target, IP, port, name, password, timeout=1) is similar to verifyhaslocal, except that it is executed at the far end.
- The Verifysumremote (self, cmd, target, SUM, IP, port, name, password, timeout=10) and verifysumlocal functions are similar, except for the remote execution
- 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:
- 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