When testing your application, you’ll often come across the need to test that your application behaves correct across a range of inputs or conditions. Instead of repetitively defining the same test scaffolding, let’s use some meta-programming to build our tests methods from a list of parameters. Nose has support for Generator Tests, but they don’t work with python’s unittest
framework. Let’s walk-through how to build our own test generator that does.
Our Test Case
So let’s write a simple application for demonstration. We want to see if our input string is alphabetized.
|
|
Now let’s write a unit test case to check our implementation:
|
|
参考资料
[1] Python 自动生成测试
[2] Python Mock 测试工具
[3] Python 常见的十个错误
[4] Python单元测试和Mock测试