SoapUi Integration with Visual Studio

今天主要简单介绍下怎么运用Visual Studio Unit Test run SoapUI Project.

1.现自行创建一个可运行的SoapUI的Project,得到项目XML文件.eg:DeviceReportService-soapui-project.xml

2.用VS创建一个Unit Test Project.添加reference,Check System, System.Configuration, System.Core, System.Data

3.添加app config文件,指定soapUI TestRunner.exe所在路径.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="SoapUIHome" value="C:Program FilesSmartBearSoapUI-5.3.0in"/>
  </appSettings>
</configuration>
View Code

添加SoapUIRunner公共类用于通过新建的Process去调用TestRunner.exe命令进而运行SoapUI的case.
为SoapUI test Suite添加Unit Test(DeviceReport.cs),将soapUI项目的XML文件加入项目中,最终项目结构如下图:

4.其中Runner SoapUI的cs源码如下:

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SoapUI
{
    public class SoapUIRunner
    {
        public void RunSoapUItest(string soapProject,string testSuiteName,string testName,string report,string set)
        {
            const string fileName = "cmd.exe";
            var soapProjectFileName = Path.GetFullPath(soapProject);

            var arguments = string.Format("/C testrunner.bat -s"{0}" -c"{1}" "{2}" -r -a -f"{3}" -t"{4}" ", testSuiteName, testName, soapProjectFileName, report, set);
            var soapHome = System.Configuration.ConfigurationManager.AppSettings["SoapUIHome"];
            //start a process and hook up the in/output
            var process = new Process
            {
                StartInfo = new ProcessStartInfo
                {
                    FileName = fileName,
                    Arguments = arguments,
                    WorkingDirectory = soapHome,
                    Verb = "runas",
                    CreateNoWindow = true,
                    ErrorDialog = false,
                    RedirectStandardError= true,
                    RedirectStandardOutput = true,
                    UseShellExecute = false

                },
                EnableRaisingEvents = true
            };

            //pipe the output to console.writeline
            process.OutputDataReceived += (sender, args) =>
              Console.WriteLine(args.Data);
            var errorBuilder = new StringBuilder();

            //store the errors in a stringbuilder
            process.ErrorDataReceived += (sender, args) =>
            {
                if (args != null && args.Data != null)
                {
                    errorBuilder.AppendLine(args.Data);
                }
            };

            process.Start();
            process.BeginOutputReadLine();
            process.BeginErrorReadLine();
            //wait for soapUI to finish
            process.WaitForExit();

            //fail the test if anything fails
            var errorMessage = errorBuilder.ToString();
            if(!string.IsNullOrEmpty(errorMessage))
            {
                Assert.Fail("Test with name '{0}' failed. {1} {2}", testName, Environment.NewLine, errorMessage);
            }
        }
    }
}
View Code

5.通过Unit Test调用SoapUI Suit的源码:

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace SoapUI
{
    [TestClass]
    [DeploymentItem(@"soapUIFilesDeviceReportService-soapui-project.xml", "TestData\soapUIFiles")]
    public class DeviceReport:SoapUIRunner
    {
        private string testCaseSuiteName = "BasicHttpBinding_DeviceReport TestSuite";
        private string soapProjectFile= @"TestData\soapUIFiles\DeviceReportService-soapui-project.xml";
        private string reportFile = @"C:Users" + Environment.UserName + "\Desktop\TestReport";
        private String SoapUISettingsFile = @"TestData\soapUIFiles\soapui-settings.xml";
        private TestContext testContext;

        public TestContext TestContext
        {
            get { return this.testContext; }
            set { this.testContext = value; }
        }

        [TestMethod]
        [TestCategory("DeviceReport")]
        public void Device_Report()
        {
            RunSoapUItest(soapProjectFile, testCaseSuiteName, "DeviceReport TestCase", reportFile, SoapUISettingsFile);
        }
    }
}
View Code

整个项目源码可以在以下路径下载:

 链接: https://pan.baidu.com/s/1hstgM7U 密码: 6sfk

原文地址:https://www.cnblogs.com/jessicaxia/p/7718298.html