Odoo 11 Development Essentials(Third Edition)
上QQ阅读APP看书,第一时间看更新

Testing business logic

Now we should add tests for the business logic. Ideally, we want every line of code to be covered by at least one test case. In tests/test_todo.py, add a few more lines of code to the test_create() method:

    def test_clear_done(self): 
"Clear Done sets Todo to not active"
Todo = self.env['todo.task']
task = Todo.create({'name': 'Test Task'})
task.do_clear_done()
self.assertFalse(task.active)

It is recommended to, as much as possible, write a different test case for each action to check. This test case begins in a similar way to the previous one: by creating a new To-Do task. This is needed because test cases are independent, and the data created or changed during one test case is rolled back when it ends. We then call the tested method on the created record, and then check the Active? flag is set to the intended value.

If we now run the tests and the model methods are correctly written, we should see no error messages in the server log.