python+selenium 組織用例方式 總結

來源:互聯網
上載者:User

標籤:poi   line   report   sele   index   ndt   getcwd   strftime   default   

 

1、unittest.main()

將一個單元測試模組變為可直接啟動並執行測試指令碼,main()方法使用TestLoader類來搜尋所有包含在該模組中以“test”命名開頭的測試方法,並自動執行他們。執行方法的預設順序是:根據ASCII碼的順序載入測試用例,數字與字母的順序為:0-9,A-Z,a-z。所以以A開頭的測試案例方法會優先執行,以a開頭會後執行。

class Test_TC_Login(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome(cc.driverPath())
self.base_url = cc.baseUrl()
self.testCaseInfo = TestCaseInfo(id=1, name="Test case name", owner=‘xua‘)
self.testResult = TestReport()
LogUtility.CreateLoggerFile("Test_TC_Login")

def test_A(self):
try:
self.testCaseInfo.starttime = cc.getCurrentTime()
# Step1: open base site
LogUtility.Log("Open Base site" + self.base_url)
self.driver.get(self.base_url)

# Step2: Open Login page
login_page = LoginPage(self.driver)

# Step3: Enter username & password
LogUtility.Log("Login web using username")
login_page.set_username("username")
login_page.set_password("password")

time.sleep(2)
# Checkpoint1: Check popup dialog title
LogUtility.Log("Check whether sign in dialog exists or not")
self.assertEqual(login_page.get_DiaglogTitle(), "Sign in")

# time.sleep(3)
# Step4: Cancel dialog
login_page.click_cancel()
self.testCaseInfo.result = "Pass"

except Exception as err:
self.testCaseInfo.errorinfo = str(err)
LogUtility.Log(("Got error: " + str(err)))
finally:
self.testCaseInfo.endtime = cc.getCurrentTime()
self.testCaseInfo.secondsDuration = cc.timeDiff(self.testCaseInfo.starttime, self.testCaseInfo.endtime)

def tearDown(self):
self.driver.close()
self.testResult.WriteHTML(self.testCaseInfo)


if __name__ == ‘__main__‘:
unittest.main()

2、批量執行:suite.addTest

    if __name__=="__main__":

         suite = unittest.TestSuite()

   suite.addTest(TestIndex("test_index")) #類裡的某個測試方法

      suite.addTest(TestSys(‘test_ddt‘))

         runner=unittest.TextTestRunner()

         runner.run(suite)

   這樣你可以在一個檔案裡面執行所有的用例,也可以使用“#”注釋掉當前不需要執行的那條用例

 

3、unittest.defaultTestLoader.discover

   defaultTestLoader()類,通過該類下面的discover()方法可自動更具測試目錄start_dir匹配尋找測試案例檔案(test*.py),並將尋找到的測試案例組裝到測試套件,因此可以直接通過run()方法執行discover。用法如下:

discover=unittest.defaultTestLoader.discover(test_dir, pattern=‘test_*.py‘)

if __name__ == "__main__":
# 設定報告檔案儲存路徑
report_path = os.getcwd() + ‘/test_report/‘
# 擷取系統目前時間
now = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time()))
# 設定報告名稱格式
HtmlFile = report_path + now+"自動化測試報告.html"
fp = open(HtmlFile, "wb")

suiteA = unittest.TestLoader().loadTestsFromTestCase(TestSys)
suiteB = unittest.TestLoader().loadTestsFromTestCase(TestAuth)
suite = unittest.TestSuite([suiteA, suiteB])

  runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title=u"商戶管理系統測試報告", description=u"用例測試情況")
#runner = unittest.TextTestRunner()
runner.run(suite)

 

python+selenium 組織用例方式 總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.