python单元测试之unittest

作为测试出身,怎么能错过对unnittest的研究

login_test_empty.json

{
  "test_login_01": {
  "username": "lmh",
  "password": "",
    "assert_info":"用户名或密码不能为空"
},
  "test_login_02": {
  "username": "",
  "password": "",
    "assert_info":"用户名或密码不能为空"
},
  "test_login_03": {
  "username": "",
  "password": "lmh",
    "assert_info":"用户名或密码不能为空"
}
}

import unittest
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from ddt import ddt,file_data


@ddt
class Test_login(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()  # 普通模式
        self.driver.get("http://sharey.notefirst.com/Account/Login")
        '''
        #无头模式
        chrome_options = Options()
        chrome_options.add_argument('--headless')
        driver = webdriver.Chrome(chrome_options=chrome_options)
        '''

    def tearDown(self):
        self.driver.quit()

    @file_data("./login_test_empty.json")
    def test_login(self,username,password,assert_info):
        self.driver.find_element_by_class_name('login_input').send_keys(username)
        self.driver.find_element_by_name('Password').send_keys(password)
        self.driver.find_element_by_class_name('login_button').click()
        error_info = self.driver.find_element_by_class_name('validation-summary-errors').text.strip()
        self.assertEqual(error_info,assert_info)

if __name__ == '__main__':
    unittest.main(verbosity=2)

posted @ 2018-09-28 19:02  mengnan254  阅读(131)  评论(0编辑  收藏  举报