Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在使用python unittest运行测试方法之后添加额外的断言?_Python_Unit Testing_Pytest - Fatal编程技术网

如何在使用python unittest运行测试方法之后添加额外的断言?

如何在使用python unittest运行测试方法之后添加额外的断言?,python,unit-testing,pytest,Python,Unit Testing,Pytest,我有92个测试,我想确保在通话过程中没有出现无声错误。 不幸的是,在OpenGL中处理错误不是很好。我想测试glGetError()是否返回其他,然后GL\u NO\u ERROR如果我每个TestCase测试一次就足够了。如果我能在每个测试方法之后添加一个断言,那就更好了。(我不想在92方法中手动添加它) 我制作了一个示例片段,其中显示了一个不可接受的解决方案,因为断言是在tearDownClass(cls)方法中完成的,tearDownClass不应该执行任何测试逻辑 如何在测试后添加额外的

我有92个测试,我想确保在通话过程中没有出现无声错误。 不幸的是,在OpenGL中处理错误不是很好。我想测试
glGetError()
是否返回其他,然后
GL\u NO\u ERROR
如果我每个
TestCase
测试一次就足够了。如果我能在每个测试方法之后添加一个断言,那就更好了。(我不想在92方法中手动添加它)

我制作了一个示例片段,其中显示了一个不可接受的解决方案,因为断言是在
tearDownClass(cls)
方法中完成的,
tearDownClass
不应该执行任何测试逻辑

如何在测试后添加额外的断言?

带注释的行显示了我不想实现的目标

注意:

cls.ctx.error
是一个属性(
glGetError()
作为字符串),可能的值为:

"GL_NO_ERROR"
"GL_INVALID_ENUM"
"GL_INVALID_VALUE"
"GL_INVALID_OPERATION"
"GL_INVALID_FRAMEBUFFER_OPERATION"
"GL_OUT_OF_MEMORY"
"GL_STACK_UNDERFLOW"
"GL_STACK_OVERFLOW"
"GL_UNKNOWN_ERROR"

您可以在tearDown(与tearDownClass相反)方法中进行测试,因为这是一个常规实例方法:

class TestCase(unittest.TestCase):

    def setUp(self):
        self.ctx = ModernGL.create_standalone_context()

    def tearDown(self):
        error = self.ctx.error                   # Store error in a variable
        self.ctx.release()                       # Then release the context
        self.assertEqual(error, 'GL_NO_ERROR')   # Check if there were errors before the release
最后,我使用
tearDown()
检查无提示错误。
class TestCase(unittest.TestCase):

    def setUp(self):
        self.ctx = ModernGL.create_standalone_context()

    def tearDown(self):
        error = self.ctx.error                   # Store error in a variable
        self.ctx.release()                       # Then release the context
        self.assertEqual(error, 'GL_NO_ERROR')   # Check if there were errors before the release