自定义Python测试套件不过滤和运行所有案例

自定义Python测试套件不过滤和运行所有案例,python,unit-testing,test-suite,Python,Unit Testing,Test Suite,我创建了一个自定义测试套件,只运行一个测试用例,但所有测试用例都在运行 class TestBlackboxGame(unittest.TestCase): @classmethod def setUpClass(cls): cls.public_path = os.path.join('public', 'index.html') cls.game_path = os.path.abspath(os.path.join('..', cls.pu

我创建了一个自定义测试套件,只运行一个测试用例,但所有测试用例都在运行

class TestBlackboxGame(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls.public_path = os.path.join('public', 'index.html')
        cls.game_path = os.path.abspath(os.path.join('..', cls.public_path))
        assert(os.path.exists(cls.game_path))

        cls.driver = webdriver.Chrome()

    @classmethod
    def tearDownClass(cls):
        cls.driver.quit()

    def test_open_game(self):
        print('Visiting game at ' + self.game_path)
        self.driver.get(self.game_path)
        self.assertTrue('Wheel' == self.driver.title)

    def test_selenium_start_with_bing(self):
        self.driver.get("http://www.bing.com")    
        inputElement = self.driver.find_element_by_name("q")
        inputElement.send_keys("cheese!")
        inputElement.submit()
        self.assertTrue('cheese' in self.driver.title)

def testsuite_open_game():
    suite = unittest.TestSuite()
    suite.addTest(TestBlackboxGame("test_open_game"))
    return suite

if __name__ == '__main__':
    runner = unittest.TextTestRunner(failfast=True)
    runner.run(testsuite_open_game())
在我的套件中,我只添加了测试用例test_open_游戏,但它同时运行两种情况,包括去Bing和搜索。我遗漏了什么?

看看答案


似乎当您将一个测试用例添加到套件中时,您正在添加它的所有测试。如果您只想运行一个测试,请将其分为两个测试用例。

结果表明,代码正在执行它应该执行的操作。我正在使用Pycharm,但没有注意到Pycharm将脚本作为单元测试运行。当Pycharm运行这个脚本时,它会绕过我的main并运行所有测试用例。

哦,那么类TestBlackboxGame就是一个测试用例?我的理解是TestBlackboxGame中的每个方法都是一个单独的测试用例,除了设置和拆卸之外。相反,我是一个白痴,在Pycharm中运行它,它认识到它是单元测试,并通过我的main.so有效地运行所有这些。您是如何使其作为单个测试运行的?您可以右键单击要运行的测试函数,Pycharm将为您提供仅运行该测试的选项。