【问题标题】:Why is setUpClass(cls): executed several times?为什么 setUpClass(cls): 执行了几次?
【发布时间】:2016-06-15 10:55:28
【问题描述】:

我正在为 Web 服务实现组件测试。我想使用类方法setUpClass(cls): 在开始时只启动我的网络服务一次,但是setUpClass(cls): 在我的每个测试用例之前执行。为什么?文档说它在执行测试用例之前只执行一次。有任何想法吗? 我正在使用 Python 3.5。

class SomeTest(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls.MyService = ComponentManager.GetMyService()
        cls.MyService.Start()
        return super(SomeTest, cls).setUpClass()

    @classmethod
    def tearDownClass(cls):
        return super(SomeTest, cls).tearDownClass()

    def test_invalid_request(self):
        response = self.client.SendJSONRequest(Constants.METHOD_POST, '/tile/invalid/', '{}')
        ...

【问题讨论】:

  • 可以添加一个测试用例吗?
  • 您使用的是什么测试运行器?该测试运行程序是否同时执行测试?
  • @wind85:我添加了一个测试用例。我所有的测试用例都是这样的。 (总是以 test_ 开头)
  • @Javiator 不应该使用 setUp() 方法吗?
  • @MartijnPieters 我不知道。我正在使用 VS 2013 和 Anaconda 3。在哪里可以查看我正在使用的测试运行器?

标签: python unit-testing python-3.x visual-studio-2013


【解决方案1】:

查看Python tools for Visual Studio source code,VS 测试运行器完全孤立地运行每个测试用例

每个测试用例都作为 Python 解释器的单独命令行调用运行(使用custom test launcher,为每个测试传递模块和测试名称),这就是为每个测试调用类设置和拆卸的原因. Python 从来没有机会一起运行这些测试。

如果这对你很重要,你必须file a ticket with the Python Tools project;我现在看不到relevant open ticket for the test adapter

或者,使用不同的测试运行程序运行您的测试。您可以使用built-in unittest command line,例如:

python3 -m unittest your_package

或选择popular tools available中的任何一个。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多