从Windows主机在远程Linux机器上运行部分Python单元测试

时间:2015-08-19 07:54:01

标签: python linux windows pytest xdist

我的情况是这样的:

  • 我有一个基于Windows的服务器程序和一个基于Linux的客户端。
  • 我对运行并需要在本地Linux机器上运行的Linux客户端进行了很多测试
  • 我需要从Windows服务器机器运行一些代码,它会向linux客户端发送一些消息。然后,应在linux客户端机器上执行测试,以验证这些消息的影响

因此,典型的测试用例看起来像这样,在Windows主机上运行:

test_example_message(self):
    # has to be executed locally on windows server
    send_message(EXAMPLE, hosts)
    # has to be executed locally on linux clients
    for host in hosts:
        verify_message_effect(EXAMPLE, host)

我发现 pytest-xdist 能够以某种方式做到这一点。

我有关于如何使用它的任何好的教程或代码示例吗?

2 个答案:

答案 0 :(得分:1)

我的最终设计使用了ssh&多处理而不是xdist(在 execute_tc()函数中):

import multiprocessing
import test_functions

def test_example_message(self):
"""Example test case"""
    # to get IPs, usernames, passwords, other required data
    config = get_test_config('example_message')
    # will take care of threading and executing parts
    result_dict = execute_tc(config)
    # to fail or not to fail. take care of proper reporting
    process_results(result_dict)

def execute_tc(config):
"""Execute test code in parallel"""
    # create shared results dictionary
    manager = multiprocessing.Manager()
    result_dict = manager.dict({})
    # create processes
    processes = []
    for func, platform, args in config:
        function = getattr(test_functions, func)
        worker = multiprocessing.Process(target=function, args=[result_dict, platform, args])
        worker.daemon = True
        worker.start()
        processes.append(worker)

    for process in processes:
        process.join()

    return result_dict

答案 1 :(得分:0)

def execute_tc(config):
"""Execute test code in parallel"""
    # create shared results dictionary
    manager = multiprocessing.Manager()
    result_dict = manager.dict({})
    # create processes
    processes = []
    for func, platform, args in config:
        function = getattr(test_functions, func)
        worker = multiprocessing.Process(target=function, args=[result_dict, platform, args])
        worker.daemon = True
        worker.start()
        processes.append(worker)

    for process in processes:
        process.join()

    return result_dict

我认为你已经改变了执行测试用例方法来执行特定的测试用例。