Execute script cases in bulk using the Discover method of the Python UnitTest Unit test framework

Source: Internet
Author: User

1. When automating with Python, execute a single script use case that's all so easying. When the script use cases continue to increase, it is blind, one by one to run to run out, so the Discover method role comes out, nonsense, small part directly on the code, if there is doubt, you can consult the small series

#!/usr/bin/python
#-*-Coding:utf-8-*-
From selenium import Webdriver
Import Time,os
From Selenium.webdriver.common.action_chains import Actionchains
Import Pickle
From Selenium.webdriver.support.ui import webdriverwait
From selenium.webdriver.common.by Import by
From Selenium.webdriver.common.keys import keys
From Selenium.webdriver.support.ui import Select
From selenium.common.exceptions import nosuchelementexception
Import UnitTest, Re,os
Import logging
Import Math
From Selenium.webdriver.support import expected_conditions as EC
Import time
Import Htmltestrunner
#time. Strftime ('%y-%m-%d-%h-%m ', Time.localtime (Tim.e.time ()))
Import compileall# to compile the py file into a binary PYc file
#compileall. Compile_dir (R ' H:/game ')
The #这样就把game目录, and the py file under its subdirectory, is compiled into a pyc file.

Case_dir = "C:\Python34\meizhu"

Def Creatsuitel ():
Testunit=unittest. TestSuite () #初始化一个测试套件
Discover = Unittest.defaultTestLoader.discover (Case_dir, pattern= "test*.py", Top_level_dir=none)

"""

There are three parameters in the 1.discover method:

-case_dir: This is the directory where the use case is to be executed.

-pattern: This is the rule that matches the script name, test*.py means all the scripts that match the beginning of the test.

-top_level_dir: This is the name of the top-level directory, the general default is equal to none on the line.

The use case that 2.discover loads into is a list collection that needs to be re-written to a list object testcase, so that it can be executed using the Run method of the Texttestrunner class here in UnitTest.

"""
For Test_suite in Discover:

For Meizhu in Test_suite:
Testunit.addtests (Meizhu)
Print (Testunit)
Return (Testunit)

#利用for循环遍历匹配已test开头脚本文件, and then load into the test suite

#alltestnames = Creatsuitel ()
if __name__ = = "__main__":
#runner = UnitTest. Texttestrunner ()
now = Time.strftime ('%y-%m-%d-%h-%m ', Time.localtime (Time.time ())) #输出当前时间
Fp=open ("Result" +now+ ". html", ' WB ')
Runner=htmltestrunner.htmltestrunner (Stream=fp,title=u ' use case implementation ', Description=u ' report: ')
Runner.run (Creatsuitel ())
Fp.close ()

Execute script cases in bulk using the Discover method of the Python UnitTest Unit test 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.