gpt4 book ai didi

nose - 在并行 python 中运行测试用例

转载 作者:行者123 更新时间:2023-12-04 14:51:52 25 4
gpt4 key购买 nike

我想获得一些使用多处理模块并行运行多个 python 测试用例的帮助。我创建了一个类 FooTest,其中包含 10 个测试用例(testA、testB...)。类外有测试用例test_foo。

如何使用 python 多处理模块并行运行所有测试用例?感谢您的帮助。

import unittest
import time

def setUp():
print "module setup"

def test_foo():
pass

class FooTest(unittest.TestCase):

# preparing to test
def setUp(self):
""" Setting up for the test """
print "FooTest:setUp_:begin"

# ending the test
def tearDown(self):
"""Cleaning up after the test"""
print "FooTest:tearDown_:begin"

# test routine A
def testA(self):
"""Test routine A"""
print "FooTest:testA"
time.sleep(2)

# test routine B
def testB(self):
"""Test routine B"""
print "FooTest:testB"

# test routine C
def testC(self):
"""Test routine C"""
print "FooTest:testC"

# test routine D
def testD(self):
"""Test routine D"""
print "FooTest:testD"

# test routine E
def testE(self):
"""Test routine E"""
print "FooTest:testE"
time.sleep(2)

# test routine F
def testF(self):
"""Test routine F"""
print "FooTest:testF"

# test routine G
def testG(self):
"""Test routine G"""
print "FooTest:testG"

# test routine H
def testH(self):
"""Test routine H"""
print "FooTest:testH"

# test routine I
def testI(self):
"""Test routine I"""
print "FooTest:testI"

# test routine J
def testJ(self):
"""Test routine J"""
print "FooTest:testJ"
time.sleep(2)

最佳答案

根据 nose documentation ,您也可以通过运行 nosetests --help 获得:

--processes=NUM Spread test run among this many processes. Set a number equal to the number of processors or cores in your machine for best results. Pass a negative number to have the number of processes automatically set to the number of cores. Passing 0 means to disable parallel testing. Default is 0 unless NOSE_PROCESSES is set. [NOSE_PROCESSES]

因此,只需运行 nosetests --processes=-1 即可使用计算机上的所有内核并行运行测试。

关于nose - 在并行 python 中运行测试用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34381970/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com