简体   繁体   English

如何断言python unittest没有引发错误

[英]How to assert that an error is not raised with python unittest

So I am trying to import a module, and test methods from a class in that module. 所以我试图导入一个模块,并从该模块中的类测试方法。

Here is an example of a method. 这是方法的示例。

def production_warning(self, targetenv):
    if targetenv == 'prdv':
        prodwarning1 = raw_input("WARNING: You are deploying to the production environment.  Are you sure you want to do this?   Y/N:  ").upper()
        if prodwarning1 == "N":
            sys.exit()
        prodwarning2 = raw_input("DEPLOYING TO PRDV, ARE YOU REALLY, REALLY SURE?  Y/N:  ").upper()
        if prodwarning2 == "N":
            sys.exit()
    else:
        return True

Here is an example of a test I am trying to write. 这是我尝试编写的测试示例。

def production_warning():
    try:
        assert test.production_warning('prdv') is not errors
        assert test.validate_params('fakeenv') is errors
        print "Test Passed {0}/5: validate_params".format(counter)
        test_db_refresh()
    except:
        print "Test Failed {0}/5: validate_params".format(counter)
        test_db_refresh()

def db_refresh_prompt():
    # assert test.db_refresh_prompt() is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format(counter)

production_warning()
db_refresh_prompt()
etc()

How do I check if an error is raised? 如何检查是否出现错误? At the end of the day I'm trying to run through all of these tests and for each function, if no exceptions are raised, print "Success". 在一天结束时,我将尝试完成所有这些测试,并且对于每个功能,如果没有引发异常,请打印“成功”。 If an exception is raised, move on to the next test. 如果引发异常,请继续进行下一个测试。 People seem to keep pointing me in the direction of "calling your function will automatically raise an exception if there is one", but this will stop my test whenever an exception is thrown and I don't want that, I want to continue on to the next test. 人们似乎总是朝着“如果有一个异常,调用您的函数会自动引发异常”的方向指责我,但这会在抛出异常且我不希望出现异常时停止我的测试,我想继续下一个测试。

I can work around this by doing: 我可以通过以下方法解决此问题:

def validate_params():
try:
    assert test.validate_params('hackenv-re', 'test.username') is not errors
    assert test.validate_params('fakeenv', 'test.username') is errors
    assert test.validate_params('hackevn-re', 'vagrant') is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format(counter)
    test_db_refresh()
except:
    print "Test Failed {0}/5: validate_params".format(counter)
    test_db_refresh()

but it seems like that defeats the purpose of using unittest in the first place? 但这似乎打败了首先使用unittest的目的? I thought with unittest I can just assert if an exception is raised and it returns a T/F that I can do whatever I want with. 我认为使用unittest可以断言是否引发异常,并且它会返回一个T / F,我可以执行任何我想做的事情。

Hope that is enough information. 希望有足够的信息。

Based on many of the answers given, I'm assuming there is nothing built in to unittest where I can do assertRaise (I believe this is used in Django) 根据给出的许多答案,我假设没有任何内置工具可以在哪里测试我可以做的assertRaise(我相信这是在Django中使用的)

断言,经过测试的代码不会引发异常是免费的,您无需为此编写代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM