예제 #1
0
class RunTest(object):
    def __init__(self):
        self.log = MyLog().get_logger()
        self.result_path = MyLog().get_result_path()
        self.case_list_file = os.path.join(ReadConfig.conf_path, "caselist.txt")
        self.case_file = os.path.join(ReadConfig.proDir, "test_case")
        self.case_list = []

    def get_case_list(self):
        with open(self.case_list_file) as f:
            for case in f.readlines():
                dcase = case.replace('\n', '')
                if not dcase.startswith('#') and dcase != '':
                    self.case_list.append(dcase)

        return self.case_list

    def get_case_suite(self):
        self.get_case_list()
        test_suite = unittest.TestSuite()
        suite_module = []
        for case in self.case_list:
            discover = unittest.defaultTestLoader.discover(self.case_file, pattern=case + ".py", top_level_dir=None)
            suite_module.append(discover)

        # length jugment have problem
        if len(suite_module) > 0:
            for suite in suite_module:
                for test_name in suite:
                    test_suite.addTest(test_name)
        else:
            return None
        return test_suite

    def run(self):
        try:
            suite = self.get_case_suite()
            print ("suite:", suite)
            if suite is not None:
                self.log.info("*************TEST START*************")
                f = open(self.result_path, 'wb')
                runner = HTMLTestRunner.HTMLTestRunner(stream=f, title='Test Report', description='Test Description', verbosity=2)
                runner.run(suite)
                f.close()
            else:
                self.log.info("Have no test case!")
        except Exception as e:
            self.log.error(str(e))
        finally:
            self.log.info("*************TEST END***************")
예제 #2
0
class TestInterface(unittest.TestCase):
    def setUp(self):
        self.log = MyLog().get_logger()
        self.log.info("Begin test!!!")
        self.mysql = MyDb.Mysql()

    def test_insert_one(self):
        sql = "insert into t_employee_info(name, sex, age, position, adress, description, update_time) values(%s, %s, %s, %s, %s, %s, %s)"
        dt = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        print dt
        params = ('李俊', '男', 26, '测试工程师', '雨花台区安德门龙福山庄', '门户活动测试工作', dt)
        self.mysql.insert_one(sql, params)
        self.mysql.dispose()
        try:
            self.assertEqual(1, 2, msg="insert one Failed")
        except Exception as err:
            self.log.error(err)

    def test_get_one(self):
        sql = "select * from test.t_employee_info"
        a = self.mysql.get_one(sql)
        self.mysql.dispose()
        for k, value in a.items():
            print k, value

    def test_get_many(self):
        sql = "select * from test.t_employee_info"
        a = self.mysql.get_many(sql, 2)
        self.mysql.dispose()
        for i in a:
            for k, value in i.items():
                print k, value

    def test_update(self):
        sql = 'update test.t_employee_info set name = \'李四\' where id = 1'
        self.mysql.update(sql)
        self.mysql.dispose()

    def test_insert_many(self):
        sql = "insert into t_employee_info(name, sex, age, position, adress, description, update_time) values(%s, %s, %s, %s, %s, %s, %s)"
        dt = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        print dt
        params = ('刘飞', '男', 26, '测试工程师', '雨花台区安德门龙福山庄', '门户活动测试工作', dt)
        self.mysql.insert_many(sql, params)
        self.mysql.dispose()

    def test_getdata_xls(self):
        for s in GetData.get_xls("test_case.xlsx", "mycase"):
            print s

    # def test_get_cookies(self):
    #     ht = Http()
    #     headers = {"Accept": "application/json, text/javascript, */*; q=0.01",
    #                "Accept-Encoding": "gzip, deflate",
    #                "Accept-Language": "zh-CN,zh;q=0.8",
    #                "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
    #                              "Chrome/58.0.3029.81 Safari/537.36",
    #                'X-Requested-With': 'XMLHttpRequest',
    #                "Content-Type": "application/json;charset=UTF-8"
    #                }
    #     ht.set_headers(headers)
    #     ht.set_url("/icompaign/getVerifyCode")
    #     gr = ht.get()
    #     jsessionid = GetData.get_cookies(ht.url, "get", data=None)
    #     print gr.status_code
    #     print jsessionid
    #     cookies = {"JSESSIONID": jsessionid}
    #     ht.set_cookies(cookies)
    #
    #     ht.set_url("/icompaign/loginByPhone.view")
    #     data = {"phone": "18626330613", "code": "111111", "vCode": "4jjm"}
    #     data = json.dumps(data)
    #     ht.set_data(data)
    #     r = ht.post()
    #     print r.status_code
    #     print r.text

    def tearDown(self):
        self.log.info("End test!!!")