如何在循环中运行多个 Python 测试用例?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19071601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I run multiple Python test cases in a loop?
提问by picardo
I am new to Python and trying to do something I do often in Ruby. Namely, iterating over a set of indices, using them as argument to function and comparing its results with an array of fixture outputs.
我是 Python 新手,正在尝试做一些我经常在 Ruby 中做的事情。也就是说,迭代一组索引,将它们用作函数的参数并将其结果与一组夹具输出进行比较。
So I wrote it up like I normally do in Ruby, but this resulted in just one test case.
所以我像往常一样在 Ruby 中编写它,但这导致只有一个测试用例。
def test_output(self):
for i in range(1,11):
....
self.assertEqual(fn(i),output[i])
I'm trying to get the test for every item in the range. How can I do that?
我正在尝试对该范围内的每个项目进行测试。我怎样才能做到这一点?
采纳答案by Trevor Merrifield
Using unittest you can show the difference between two sequences all in one test case.
使用 unittest,您可以在一个测试用例中显示两个序列之间的差异。
seq1 = range(1, 11)
seq2 = (fn(j) for j in seq1)
assertSequenceEqual(seq1, seq2)
If that's not flexible enough, using unittest, it is possible to generate multiple tests, but it's a bit tricky.
如果不够灵活,使用unittest,可以生成多个测试,但是有点棘手。
def fn(i): ...
output = ...
class TestSequence(unittest.TestCase):
pass
for i in range(1,11):
testmethodname = 'test_fn_{0}'.format(i)
testmethod = lambda self: self.assertEqual(fn(i), output[i])
setattr(TestSequence, testmethodname, testmethod)
Nose makes the above easier through test generators.
Nose 通过测试生成器使上述操作更容易。
import nose.tools
def test_fn():
for i in range(1, 11):
yield nose.tools.assert_equals, output[i], fn(i)
Similar questions:
类似问题:
回答by spinus
In python world two most popular options to write tests are:
在 python 世界中,编写测试的两个最流行的选项是:
In pytest you parametrize tests very easly:
在 pytest 中,您可以非常轻松地对测试进行参数化:
@pytest.mark.parametrize(('param1', 'param2'),[
(1, 'go'),
(2, 'do not go')])
def test_me(param1, param2):
# write test
This will produce nice output also while running tests:
这也会在运行测试时产生不错的输出:
go.py:2: test_me[1-go] PASSED
go.py:2: test_me[2-do not go] PASSED
I am using pytest for two years now and it's very nice tool. You have many features there. Besides parametrization there are fixtures also, very very nice assertions (you do not need to write assertEqual, just assert a==b
and pytest can generate very nice and helpful output for it.)
我现在使用 pytest 两年了,它是一个非常好的工具。你有很多功能。除了参数化之外,还有固定装置,非常非常好的断言(您不需要编写 assertEqual,只需assert a==b
pytest 可以为它生成非常好的和有用的输出。)
回答by Antoine Fontaine
Starting from python 3.4, you can do it like this:
从python 3.4开始,你可以这样做:
def test_output(self):
for i in range(1,11):
with self.subTest(i=i):
....
self.assertEqual(fn(i),output[i])
回答by Sourav Choudhary
If Your question is regarding, when you are solving the problem on competitive places like hackerrank or anywhere else. If they have not provided their environment to run test cases in a loop.
如果您的问题涉及,当您在像hackerrank 或其他任何地方这样的竞争场所解决问题时。如果他们没有提供他们的环境来循环运行测试用例。
And locally if You are running code for python compiler It would be useful.
如果您在本地运行 python 编译器的代码,这将很有用。
you can simply use a while loop or range function of python.
您可以简单地使用python的while循环或范围函数。
e.g:
例如:
t = int(input("Enter Number of testcases"))
type(t)
while(t!=0):
n = int(input("Enter number of data"))
type(n)
// Code logic or function Call
t = t-1