• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
pealy
博客园    首页    新随笔    联系   管理    订阅  订阅

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 Files\SmartBear\SoapUI-5.3.0\bin"/>
  </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(@"soapUIFiles\DeviceReportService-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

posted @ 2017-10-23 18:32  jessicaxia  阅读(317)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3