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

时间:2017-06-07 12:54:48

标签: python unit-testing pytest

我有 92 测试,我想确保在通话过程中没有发生无声错误。 不幸的是,错误处理OpenGL并不是很好。我想测试glGetError()是否返回其他GL_NO_ERROR如果我按TestCase测试一次就足够了。如果我可以在每个测试方法之后添加断言会更好。 (我不想在92种方法中手动添加它)

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

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

  

带有评论的行显示了我不想实现的目标。

import struct
import unittest

import ModernGL


class TestCase(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls.ctx = ModernGL.create_standalone_context()

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

    def test_1(self):
        ...

    def test_2(self):
        ...

    def test_3(self):
        ...            


if __name__ == '__main__':
    unittest.main()

注意:

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"

1 个答案:

答案 0 :(得分:2)

你可以在tearDown(而不是tearDownClass)方法中进行测试,因为这是一个常规实例方法:

function book_appointment_timeslot(job_id,planner_date,time_slot,contract_id){    
   $.ajax({
              type: "POST",
              url: "/add_appointment_planner",
              data: { contract_id: contract_id,job_id : job_id,  planner_date: planner_date, time_slot: time_slot, _token: CSRF_TOKEN },  
              success: function(data){
                     if(data['status']=='success')
                     {
                            show_notification(data['status'],data['message']);
                            $("#apt_status_table").load(location.href + " #apt_status_table");
                            $(".tbl_planner_list_whole").load(location.href + " .tbl_planner_list_whole");
                            $('.appointment_edit_button').click(function(){alert("success");});

                     }
              }
});
}