Python unittest:如果特定测试失败,则取消所有测试 [英] Python unittest: cancel all tests if a specific test fails

查看:269
本文介绍了Python unittest:如果特定测试失败,则取消所有测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用unittest来测试我的Flask应用程序,并使用nose来实际运行测试.

I am using unittest to test my Flask application, and nose to actually run the tests.

我的第一组测试是确保测试环境整洁,并防止在Flask应用程序的已配置数据库上运行测试.我相信我已经干净地设置了测试环境,但是我希望在不运行所有测试的情况下对此有所保证.

My first set of tests is to ensure the testing environment is clean and prevent running the tests on the Flask app's configured database. I'm confident that I've set up the test environment cleanly, but I'd like some assurance of that without running all the tests.

import unittest

class MyTestCase(unittest.TestCase):
    def setUp(self):
        # set some stuff up
        pass

    def tearDown(self):
        # do the teardown
        pass

class TestEnvironmentTest(MyTestCase):
    def test_environment_is_clean(self):
        # A failing test
        assert 0 == 1

class SomeOtherTest(MyTestCase):
    def test_foo(self):
        # A passing test
        assert 1 == 1

我希望TestEnvironmentTest导致unittestnose失败时保释,并防止SomeOtherTest和任何进一步的测试运行.在unittest(首选)或nose中是否有这样做的内置方法?

I'd like the TestEnvironmentTest to cause unittest or noseto bail if it fails, and prevent SomeOtherTest and any further tests from running. Is there some built-in method of doing so in either unittest (preferred) or nose that allows for that?

推荐答案

为了使一个测试首先执行,并且仅在其他测试出现错误的情况下停止其他测试的执行,您需要放置一个在 setUp() 中调用测试(因为python不保证测试顺序),然​​后失败或跳过其余的失败内容.

In order to get one test to execute first and only halt execution of the other tests in case of an error with that test, you'll need to put a call to the test in setUp() (because python does not guarantee test order) and then fail or skip the rest on failure.

我喜欢 skipTest() ,因为它实际上并不运行其他测试,而引发异常似乎仍然尝试运行测试.

I like skipTest() because it actually doesn't run the other tests whereas raising an exception seems to still attempt to run the tests.

def setUp(self):
    # set some stuff up
    self.environment_is_clean()

def environment_is_clean(self):
    try:
        # A failing test
        assert 0 == 1
    except AssertionError:
        self.skipTest("Test environment is not clean!")

这篇关于Python unittest:如果特定测试失败,则取消所有测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆