In order to be more worthy of the "automated testing" name, we can set up timed tasks, so that our automated script at some point in time to automatically run the script, so that the test can be done at night, reducing the time cost. The simplest way to control when the test case executes is through a program.
Borrow http://blog.csdn.net/liujingqiu/article/details/50518776 about Htmltestresult script, then write settime_autorun.py to implement scheduled tasks.
The settime_autorun.py script is as follows:
Import UnitTestImport HtmltestrunnerImport OSImport timelistaa=' C:\\python34\\test_case '#设置脚本所在的绝对路径DefCreatesuitel(): Testunit=unittest. TestSuite () discover=unittest.defaulttestloader.discover (Listaa, pattern=' unittesthtml_*.py ', top_level_dir=None)For Test_suiteIn Discover:For Test_caseIn Test_suite:testunit.addTests (test_case)Print (Testunit)Return Testunitalltestnames=createsuitel ()#now =time.strftime ('%y-%m-%m-%h_%m_%s ', Time.localtime (Time.time ())) #时间格式有错误now =time.strftime ('%y-%m-%d-%h_%m_%s ', Time.localtime (Time.time ()))#设置时间格式fp = open (now+' Result.html ',' WB ') Runner=htmltestrunner.htmltestrunner (stream=fp,title= ' test result ', Description=u ' Result: ') #调用HTMLRestRunnerk =1while k< 2:timing=time.strftime ( '%h_%m ', Time.localtime (Time.time ()) ) if timing = ' 17_35 ': #17_35指17 : 35, this can be set according to the time print ( ' start to run scripts ') Runner.run ( Alltestnames) #运行所有的case print ( Finish runing scripts ') break else:time.sleep (3) print (Timing) fp.close ()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21st
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21st
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
F5, run Get:
1) Pythonidle:
We can see that 17_34 has not yet arrived at the time point we set 17_35, so we will wait.
2) C:\Python34 path, there is result.html
2016-03-34-17_34_59result.html is our test result.html.
3) Open the above file to get:
The above three results, that is, the results obtained after the run. Here lazy, which uses pattern= ' unittesthtml_*.py ', using a different file name of the. Py script, but the contents are the same, so the same, get the results of 6 case.
Report:
1.pattern= ' unittesthtml_*.py ' means that in the current directory, the script with the file name unittesthtml_*.py will be executed in bulk.
During the implementation process, the issue encountered:
1. For the first output, the file size of 2016-03-34-17_34_59result.html in the C:\Python34 directory is 0kb, which is due to forgetting to add fp.close ().
2. The above script also has a little problem we get the 2016-03-34-17_34_59result.html file name, you can see the file name in the setting time is not correct. According to the following changes, run again, the display is correct.
The script changes are as follows:
以前的脚本:now=time.strftime(‘%Y-%m-%M-%H_%M_%S‘,time.localtime(time.time())) 改动后的脚本:now=time.strftime(‘%Y-%m-%d-%H_%M_%S‘,time.localtime(time.time())) #设置时间格式
In this way, the use of scripts to complete the timing control tasks, the implementation of timed automated testing.
Selenium+webdriver+python Timing Control Tasks