我有以下目录布局:
runner.py
lib/
tests/
testsuite1/
testsuite1.py
testsuite2/
testsuite2.py
testsuite3/
testsuite3.py
testsuite4/
testsuite4.py
Run Code Online (Sandbox Code Playgroud)
testsuite*.py模块的格式如下:
import pytest
class testsomething:
def setup_class(self):
''' do some setup '''
# Do some setup stuff here
def teardown_class(self):
'''' do some teardown'''
# Do some teardown stuff here
def test1(self):
# Do some test1 related stuff
def test2(self):
# Do some test2 related stuff
....
....
....
def test40(self):
# Do some test40 related stuff
if __name__=='__main()__'
pytest.main(args=[os.path.abspath(__file__)])
我遇到的问题是我想并行执行'testsuites',即我希望testsuite1,testsuite2,testsuite3和testsuite4并行开始执行,但是测试套件中的单个测试需要连续执行.
当我使用py.test中的'xdist'插件并使用'py.test -n 4'启动测试时,py.test正在收集所有测试并随机地在4个工作者之间平衡测试.这导致在'testsuitex.py'模块中每次测试时都会执行'setup_class'方法(这违背了我的目的.我希望每个类只执行一次setup_class,然后在那里连续执行测试).
基本上我想要的执行看起来像是:
worker1: executes all …
我正在使用pytest和pytest-xdist进行并行测试运行.-s在运行测试时,似乎没有遵守将标准输出传递到终端的选项.有没有办法让这种情况发生?我意识到这可能会导致不同进程的输出在终端中混乱,但我很好.
我想列出在会话结束时未使用的所有测试。
Pytest允许您定义一个hook pytest_sessionfinish(session, exitstatus),在会话结束时调用该钩子,我希望在该列表中找到该钩子。
session是_pytest.main.Session具有属性items(type list)的实例,但是我无法找到item该列表中的每一个是否传递失败。
使用pytest-xdist插件时该如何完成,我想在主流程中获取该列表。使用此插件,session甚至items在母版中都没有属性:
def pytest_sessionfinish(session, exitstatus):
if os.environ.get("PYTEST_XDIST_WORKER", "master") == "master":
print(hasattr(session, "items")) # False
Run Code Online (Sandbox Code Playgroud)
我正在使用py.test来运行测试.我正在使用它与pytest-xdist并行运行测试.我想在我的测试中看到print语句的输出.
我有:Ubuntu 15.10,Python 2.7.10,pytest-2.9.1,pluggy-0.3.1.
这是我的测试文件:
def test_a():
print 'test_a'
def test_b():
print 'test_b'
Run Code Online (Sandbox Code Playgroud)
当我运行py.test时,没有打印任何内容.这是预期的:默认情况下,py.test捕获输出.
当我运行py.test -s时,它会打印test_a和test_b.
当我运行py.test -s -n2时,再没有打印任何内容.如何在使用-n2时使print语句正常工作?
我已经阅读了pytest + xdist而没有捕获输出和这个错误报告.
我想py.test与hunter结合使用:
PYTHONHUNTER="module_startswith='foo'" py.test -s -k test_bar
Run Code Online (Sandbox Code Playgroud)
不幸的是,hunter的输出(跟踪)不可见。
版:
foo_cok_d@aptguettler:~$ py.test --version
This is pytest version 3.4.2, imported from /home/foo_cok_d/local/lib/python2.7/site-packages/pytest.pyc
setuptools registered plugins:
pytest-xdist-1.22.2 at /home/foo_cok_d/local/lib/python2.7/site-packages/xdist/looponfail.py
pytest-xdist-1.22.2 at /home/foo_cok_d/local/lib/python2.7/site-packages/xdist/plugin.py
pytest-forked-0.2 at /home/foo_cok_d/local/lib/python2.7/site-packages/pytest_forked/__init__.pyc
Run Code Online (Sandbox Code Playgroud)
在更简单(较小)的virtualenv中,它可以工作(相同的pytest版本,但没有插件)。
可能是什么原因?
如何调试呢?
我正在尝试使用 pytest-xdist 以使我的测试并行运行,问题是每个线程都将转到与所有测试共享的夹具并根据线程数执行它。
它给我带来了一个问题,因为该夹具的作用是为我的测试创建数据,一旦它已经创建,我就会得到和错误,因为它已经创建(通过 REST)。
conftest.py:
lock = threading.Lock()
@pytest.fixture(scope=session)
def init_data(request):
lock.acquire()
try:
data = []
if checking_data_not_created():
data.append(some_rest_command_creating_data_1())
data.append(some_rest_command_creating_data_2())
finally:
lock.release()
yield data
lock.acquire()
try:
remove_all_data()
finally:
lock.release()
Run Code Online (Sandbox Code Playgroud)
测试类.py:
class classTests():
def first_test(init_data):
test body and validation....
def second_test(init_data):
test body and validation....
Run Code Online (Sandbox Code Playgroud)
我正在使用命令:pytest -v -n2
假设第一个线程应该运行 first_test() 并且第二个线程应该运行 second_test() 其中一个总是会失败,因为第一个已经在夹具部分创建了数据,另一个线程将出现异常并且他应该运行的所有测试都将失败。
如您所见,我尝试使用锁来同步线程,但它也不起作用。
知道如何克服这个问题吗?
谢谢。
我尝试使用 pytest-xdist 在我的硒网格上并行运行 pytest。在使用 pytest-xdist 之前,我在所有测试开始使用 pytest_sessionstart 挂钩之前执行了一次设置。它工作得很好。这将是第一个运行的事情,在它完成之前不会开始任何测试。一旦我尝试使用 pytest-xdist,所有会话范围的挂钩(例如 pytest_sessionstart 和 pytest_sessionfinish)都会被执行多次。我不明白或做错了什么?
我已经在一个正常工作的pytest环境中安装了pytest-xdist:
pip install pytest-xdist
Run Code Online (Sandbox Code Playgroud)
我收到了这个输出
Downloading/unpacking pytest-xdist
Downloading pytest-xdist-1.10.tar.gz
Running setup.py egg_info for package pytest-xdist
no previously-included directories found matching '.hg'
Downloading/unpacking execnet>=1.1 (from pytest-xdist)
Downloading execnet-1.2.0.tar.gz (163kB): 163kB downloaded
Running setup.py egg_info for package execnet
warning: no files found matching 'conftest.py'
Requirement already satisfied (use --upgrade to upgrade): pytest>=2.4.2 in /Users/sal/Documents/code/Python/VirtualEnv/Spring/lib/python2.7/site-packages (from pytest-xdist)
Requirement already satisfied (use --upgrade to upgrade): py>=1.4.20 in /Users/sal/Documents/code/Python/VirtualEnv/Spring/lib/python2.7/site-packages (from pytest>=2.4.2->pytest-xdist)
Installing collected packages: pytest-xdist, execnet
Running setup.py install for pytest-xdist
no previously-included directories found …Run Code Online (Sandbox Code Playgroud) 我最近开始使用pytest运行我的自动化测试套件。依次运行时成功完成的测试运行,现在当我使用xdist并行运行套件时随机失败。我将pytest配置如下:
[pytest]
addopts = -nauto --rerun 3 -ra --timeout=180 --junit-xml=pyresult.xml
python_files=test*.py
Run Code Online (Sandbox Code Playgroud)
随机测试将无法启动Chromedriver,并且最终将引发超时。对于其他由于种种原因失败的测试失败的情况,pytest会很乐意为我重新运行测试。但是在这种情况下,当抛出超时时,pytest输出超时然后挂起。我的测试运行永远不会结束,因此詹金斯没有得到等待的结果。完成pytest的唯一方法是手动发送控制中断。
我的驱动程序如下启动(注意,我认为我的代码不是这里的问题);
@classmethod
def open_browser(cls, browser_name, configuration, down_dir=""):
"""
Returns the driver for the required browser
"""
if browser_name == 'Chrome':
chrome_options = webdriver.ChromeOptions()
prefs = {"download.default_directory" : down_dir}
chrome_options.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome(
executable_path=configuration, chrome_options=chrome_options)
Run Code Online (Sandbox Code Playgroud)
超时消息如下:
~~~~~~~~~~~~~~~~~~~~~~~~~~ Stack of <unknown> (8460) ~~~~~~~~~~~~~~~~~~~~~~~~~~~
File "c:\python27\lib\site-packages\execnet\gateway_base.py", line 277, in _perform_spawn
reply.run()
File "c:\python27\lib\site-packages\execnet\gateway_base.py", line 213, in run
self._result = func(*args, **kwargs)
File "c:\python27\lib\site-packages\execnet\gateway_base.py", line 954, in _thread_receiver
msg = Message.from_io(io) …Run Code Online (Sandbox Code Playgroud) 安装时pytest-xdist,我收到此错误:
Collecting pytest-xdist>=1.15.0
Downloading https://company.com/pypi/packages/dc/b2/a59db3ef996b93c3ef35dbbc33557a71ef67a6839d94a52c88eeb1086002/pytest-xdist-1.15.0.tar.gz (87kB)
100% |????????????????????????????????| 92kB 70.2MB/s
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/tmp/pip-build-vyEgO5/pytest-xdist/setup.py", line 37, in <module>
'Programming Language :: Python :: 3',
File "/usr/lib/python2.7/distutils/core.py", line 111, in setup
_setup_distribution = dist = klass(attrs)
File "/home/company/package_name/env/local/lib/python2.7/site-packages/setuptools/dist.py", line 318, in __init__
_Distribution.__init__(self, attrs)
File "/usr/lib/python2.7/distutils/dist.py", line 287, in __init__
self.finalize_options()
File "/home/company/package_name/env/local/lib/python2.7/site-packages/setuptools/dist.py", line 376, in finalize_options
ep.load()(self, ep.name, value)
File "build/bdist.linux-x86_64/egg/setuptools_scm/integration.py", line …Run Code Online (Sandbox Code Playgroud) pytest ×10
xdist ×10
python ×8
pytest-xdist ×1
python-2.7 ×1
python-3.x ×1
testing ×1
unit-testing ×1