问题是这样的:
定义了一个初始化类:

class SetupTeardown(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        print("用例执行前环境准备")
        # browser.open_browser()
    @classmethod
    def tearDownClass(cls):
        print("用例执行后环境还原")

再定义了两个测试类,均继承了这个初始化类:

class BaiduSearch(SetupTeardown):
    def test_baidu_search(self):
        driver = self.browser.driver
        driver.get(self.browser.url)
        driver.find_element_by_id('kw').send_keys('selenium')
        driver.find_element_by_id('su').click()
        try:
            print(driver.title)
            assert 'selenium' in driver.title
            print("Test Pass.")
        except Exception as e:
            print("Test Fail",format(e))
class Login(SetupTeardown):
    def test_login(self):
        """test case of login"""
        print('===login start===')
        self.assertEqual(4, sum(range(1,3)))

    def test_logout(self):
        """test case of logout"""
        print('===logout out===')
        self.assertTrue(1==1)

预期结果:这个初始化类应该只需要执行一次
实际结果:这个初始化类执行了两次

用例执行前环境准备
用例执行后环境还原
用例执行前环境准备
用例执行后环境还原

请问是理解有问题吗?还是代码有问题啊?谢谢大佬指点!!!


↙↙↙阅读原文可查看相关链接,并与作者交流