美文网首页
unittest实战

unittest实战

作者: 往你头上敲三下 | 来源:发表于2018-06-05 17:10 被阅读0次

    1.测试方法类

    testcase.py文件

    from selenium import webdriver

    import unittest

    import time

    class TestCaseMethod(unittest.TestCase):

    #setUpClass在所有测试用例执行之前执行,登录网站

        @classmethod

        def setUpClass(self):

            self.driver = webdriver.Chrome()

            self.driver.get('http://127.0.0.1:5000/signin')

            self.driver.find_element_by_name('username').send_keys('15090653560')

            self.driver.find_element_by_name('password').send_keys('123456')

            self.driver.find_element_by_xpath('/html/body/form/p[3]/button').click()

    # @classmethod

    # def tearDownClass(self):

    #    self.driver.close()

    #第一个测试方法,验证该页面的id = testtableafter的table标签下的tbody标签下是否有9个tr标签

        def testcase1(self):

            tr =self.driver.find_elements_by_xpath('//table[@id="testtableafter"]/tbody/tr')

            trs =len(tr)

            self.assertEqual(trs,9)

    #第二个测试方法,验证第九行第二列的超链接打开之后的title是否跟预期的title相同

    def testcase2(self):

            self.driver.find_element_by_xpath('//*[@id="testtableafter"]/tbody/tr[9]/td[2]/div/a').click()

            handles =self.driver.window_handles

            self.driver.switch_to.window(handles[2])

            time.sleep(2)

            title =self.driver.title

            self.assertEqual(title,'GitHub - jianhaohe/Python-Automation')

    2.unittest测试方法执行类

    testsuite.py文件:

    import unittest

    import time

    from testcase import TestCaseMethod

    from HTMLTestRunner_PY3import HTMLTestRunner

    if __name__ =='__main__':

        # suit = unittest.TestSuite

        # suit.addTests(unittest.TestLoader().loadTestsFromTestCase(TestCaseMethod))

        suit = unittest.TestLoader().loadTestsFromTestCase(TestCaseMethod)

        time = time.strftime('%Y-%m-%d-%H-%M',time.localtime())

        dir ='D:\\zhangcq\\0604homework\\'+time+'report.html'

        file =open(dir,'wb')

        runner = HTMLTestRunner(stream=file,title='张星测试报告',description='selenium')

        runner.run(suit)

    运行suit文件,生成测试报告

    测试报告查看:

    相关文章

      网友评论

          本文标题:unittest实战

          本文链接:https://www.haomeiwen.com/subject/xskmsftx.html