您的第一站绝对是了解您正在测试的 Web 服务。您是否有可以提交到 Web 服务的有效 XML?如果是这样,请将其存储在某处并创建一个从文件夹中获取它的步骤。使用该 XML 作为您的测试用例并遍历文件夹中的每个文件。每个 XML 文件都应该根据您希望测试涵盖的内容而有所不同。
通常,Web 服务测试将包括记录响应时间、发送不同风格的 XML 作为测试用例(更改节点的内容以涵盖不同的触发器)以及检查响应中的特定值。
一个非常高级和隐含的场景可能看起来像这样(基于我曾经做过的事情):
Scenario Outline: Submit Requests to Web Service
Given I have XML file '<XML_Case>'
And I submit a 'POST' request
Then I should receive a response from the Web Service
And the response will include a 'ResponseId' in the 'Header' section
And the response will include a 'RequestId' in the 'Body' section
And the 'Complete' node in the 'Body' section will return the value of 'True'
Examples:
|XML_Case |
| C:\TestData\test1.xml |
| C:\TestData\test2.xml |
| etc... |
为了实现这一点,C# 中有很多方法。首先,您可以检查 XML 文件并将其转换为您的 XML 请求,通过类似这样的步骤定义:
[Given(@"I have XML file '(.*)'")]
public void GivenIHaveXMLFile(string fileName)
{
//Checks if file exists
if (System.IO.File.Exists(fileName))
{
var requestXml = CreateXMLInstance(fileName);
}
else
{
throw new Exception("No XML file found in specified location");
}
}
该代码中使用的CreateXMLInstance 方法将从文件路径加载XML,可能是这样的:
public XmlDocument CreateXmlInstance(string xmlPath)
{
//Loads XML from file path
XmlDocument request = new XmlDocument();
request.Load(xmlPath);
return request;
}
您基本上已经创建了最初的几个步骤,并且可以将requestXml 变量存储为字段或Specflow 的ScenarioContext 以供以后进行Web 服务调用时使用。
显然还有很多事情需要考虑,但这可能会让您朝着正确的方向前进。