接口測試-Postman VS SoapUI

其實市面上作接口測試的工具不少,爲啥挑這兩個來說解了,重點是真心好用。好了,廢話很少說,直接上乾貨。html

相信有必定了解的人都知道這兩個工具應用最普遍的就是接口測試,既然提到接口測試那咱們不得不先普及下什麼是接口,接口測試又是啥?git

咱們常說的接口通常指下面兩種:npm

  1. API:應用程序編程接口。程序間的接口編程

  2. GUI:圖形用戶界面。人與程序的接口json

咱們這裏說的接口測試主要指API接口測試,API接口分類通常有以下幾種:後端

  • HTTP 接口服務器

  • Webservice 接口app

  • RESTful 接口ide

HTTP,RESTful 接口走 HTTP 協議,經過路徑來區分調用的方法,請求報文入參有多種形式,返回報文通常爲 json 串,最多見的是 get 和 post 方法。svn

WebService 接口是走 soap 協議,請求報文和返回報文都是 xml 格式。

而Postman和SopaUI支持的接口類型以下:

所以,咱們須要先辨別項目先後端是用何種接口交互再來選取相應的接口測試工具。

接口測試又是啥?

接口測試就是模擬客戶端向服務器發送請求報文,服務器接收請求報文後對相應的報文作處理並向客戶端返回應答,客戶端再接收應答而後對應答作一些校驗的過程。

下面咱們分別介紹下如何用PostMan和SoapUI作接口自動化測試。

A: Postman + Newman + Jenkins 實現接口自動化測試

1.安裝Postman,編寫API接口測試用例

示例:豆瓣公開查找書籍接口

2.導出Collection(項目-接口用例),安裝NewMan,用NewMan Command運行Collection並輸出HTML報告。

C:\Users\Li.Shi\AppData\Roaming\npm\newman run C:\Users\Li.Shi\Desktop\PostMan\LiShiTest.postman_collection.json --reporters cli,json --reporters cli,html --reporter-html-export htmlOut.html

3.安裝部署Jenkins,其中Jenkins的配置以下:

至此,咱們可完成基於postman和Jenkins的自動化接口測試。

B:SoapUI+UnitTest 實現接口自動化測試

1.安裝SoapUI,自行建立一個可運行的SoapUI的Project,獲得項目XML文件.eg:DeviceReportService-soapui-project.xml

2.用VS(Visual Studio)建立一個Unit Test Project.添加reference,Check SystemSystem.ConfigurationSystem.CoreSystem.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

4.添加SoapUIRunner公共類,經過新建Process去調用TestRunner.exe命令進而運行SoapUI的case.

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 TestSuit, 進而能夠運用bat命令集成運行TestCase. 作到接口的自動化測試。

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

接下來咱們對比下用方式A和B作接口自動化的區別:

1.從上面的實現來看,SoapUI自動化須要測試人員有必定的編碼能力,想好比Postman會對測試人員要求高些。

2.從兩種工具用例組織方式來看:

SoapUI的組織方式以下圖,最上層是WorkSpace,因此每一個WorkSpace中能夠打開多個Project,一個Project也能夠在不一樣的WorkSpace中。

Project對應咱們的測試項目,其中可添加WSDL、WADL資源、TestSuite以及MockService。

TestSuite對應咱們的測試模塊,好比商戶中心,其中能夠添加TestCase,TestCase對應咱們對某個模塊的不一樣接口,好比訂單管理接口。而一個接口能夠能須要多個Step完成,變量、數據源、請求等都是一個Step。

Postman功能上更簡單,組織方式也更輕量級,它主要針對的就是單個的HTTP請求。Collection就至關因而Project,而Collection中能夠建立不定層級的Folders,能夠本身組織TestSuite。每一個Request能夠當作是一個TestCase或者Step:

3. 從長期的團隊協做來看:

SoapUI:自己一個project是一個xml文件,可是能夠經過配置變成一系列文件夾,每一個Case、每一個Suite均是獨立的文件,這樣可經過svn/git/TFS進行團隊協做。支持性較好。

Postman:有團隊協做的功能,須要付費。

 所以從項目支持的接口類型,不一樣集成測試需求和後期維護來考慮,咱們能夠根據上面幾點選擇適合本身項目的接口自動化工具。

相關文章
相關標籤/搜索