使用多种设置重构鼻子测试
假设您获得以下测试代码,其中 test1
和 test2
方法对 grid
对象运行一些测试。
N = 10
grid = Grid(N)
def test1():
...
def test2():
...
当 N = 11
时添加测试以便在新对象上运行相同的方法 test1 和 test2 的最佳方法是什么?当然,可以简单地创建一个如下所示的新文件
N = 11
grid = Grid(N)
def test1():
...
def test2():
...
,但这会导致大量代码重复。
Assume you are given the following testing code, where the test1
and test2
methods run some tests on the grid
object.
N = 10
grid = Grid(N)
def test1():
...
def test2():
...
What is the best way to add tests for when N = 11
so that the same methods test1 and test2 are run on the new object? Of course one could simply create a new file like below
N = 11
grid = Grid(N)
def test1():
...
def test2():
...
but that would result in a lot of code duplication.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用全局变量是设置测试用例的一种棘手方法。您应该将测试重构为如下所示。
从那里开始,您似乎想看看 测试生成器包含在
nose
中。Using global variables is kind of a sticky way to set up test cases. You should refactor your tests into something like the following.
From there, it looks like you want to take a look at the test generators included in
nose
.