简体   繁体   English

使用Suds Python从wsdl文件生成SOAP xml

[英]Generate SOAP xml from wsdl file using Suds Python

I'm a part of testing team who uses Python. 我是使用Python的测试团队的一员。 Now we've got the requirement to automate the SOAP calls using Python. 现在我们有了使用Python自动执行SOAP调用的要求。 I have a WSDL file and we have some test data in CSV. 我有一个WSDL文件,并且我们有一些CSV测试数据。

So for any particular method, I want to fetch data from the CSV and pass it to the service as parameter for the method and trigger the call, then output the response. 因此,对于任何特定的方法,我都希望从CSV中获取数据并将其作为该方法的参数传递给服务并触发调用,然后输出响应。

The help that I wanted is can we generate the SOAP xml from the given WSDL file for a particular method? 我想要的帮助是我们可以从给定的WSDL文件为特定方法生成SOAP xml吗? If so, how can I do it? 如果是这样,我该怎么办?

If your CSV file has column headers corresponding to the arguments of the WSDL method you want to test, you can easily do that: 如果您的CSV文件具有与您要测试的WSDL方法的参数相对应的列标题,则可以轻松地做到这一点:

import csv
import suds

WSDL = 'http://some.wsdl.somewhere/'
METHOD = 'theMethodToTest'
CSVFN = '/some/csv/file.csv'

client = suds.client.Client(WSDL)

with open(CSVFN, 'rb') as f:
    reader = csv.DictReader(f)
    for kwargs in reader:
        getattr(client.service, METHOD)(**kwargs)

UPDATE 1 更新1

If you want to map a CSV flat file structure to a complex structure, then you could use the CSV headers to indicate a mapping. 如果要将CSV 平面文件结构映射到复杂结构,则可以使用CSV标头指示映射。

You could map to a dict and pass it to the WSDL method as described in the SUDS documentation: ComplexArgumentsUsingDict . 您可以映射到dict,然后将其传递给WSDL方法,如SUDS文档: ComplexArgumentsUsingDict所述

Your CSV headers could be something like: 您的CSV标头可能类似于:

person.name.first,person.name.last,person.age,person.phone.0.npa,person.phone.0.nxx,person.phone.0.number
"Elmer","Fudd",35,202,555,1212

that would be converted to: 将被转换为:

kwargs = {
    'person': {
        'name': {'first':'Elmer', 'last':'Fudd'},
        'age': 35,
        'phone': [
            {'npa':202, 'nxx':555, 'number':1212},
        ]
    }
}

I have no sample code to give you, though... :-) 我没有示例代码可以给你,但是... :-)

One issue you may also encounter is type-related. 您可能还会遇到的一个问题与类型有关。 You may have to explicitly convert data from the CSV file (text) to int, float, bool... 您可能需要将数据从CSV文件(文本)显式转换为int,float,bool ...

One idea would be to indicate the type in the headers: 一种想法是在标题中指出类型:

person.name.first,person.name.last,person.age:int,person.phone.0.npa:int,person.phone.0.nxx:int,person.phone.0.number:int,married:bool

Or: store your test data in XML... 或者:将测试数据存储为XML ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM