Uvavanyo lwe-API oluzenzekelayo lwenziwe lula ngeKarate

Ukuba ungathanda ukubandakanyeka kuvavanyo lwe-API oluzenzekelayo, kodwa ungenayo imvelaphi yenkqubo, unokufuna ukunika iKarate!

IKarate yakhutshwa njengesixhobo esivulelekileyo Intuit . Isixhobo siyilelwe ukuba sisetyenziselwe uvavanyo lwe-API oluzenzekelayo kwaye sinazo zonke izinto ezifunekayo ukwenza uvavanyo lwe-API yomoya kwaye ibe mnandi ngokwenene.

Ngokungafaniyo nezinye izixhobo zokuvavanya ze-API ezifuna inani elifanelekileyo lokufaka iikhowudi, nokuba wenze izinto ezisisiseko, iKarate isebenza ngaphandle kwebhokisi. Unokwakha ezona zinto zinzima zempendulo yesicelo ngaphandle kolwazi lwalo naluphi na ulwimi lwenkqubo. Konke okufuneka ukwenze kukubhala ifayile yefayile usebenzisa isicatshulwa esicacileyo seGherkin.


Kungenxa yokuba iKarate yiDSL epheleleyo kwaye ihlala ngaphezulu Ikhukhamba-JVM , ungaqhuba iimvavanyo kwaye uvelise iingxelo njengayo nayiphi na iprojekthi esemgangathweni yeJava, kodwa endaweni yokubhala ikhowudi yeJava, ubhala iimvavanyo ngolwimi olwenzelwe ukwenza ukujongana ne-HTTP, iJSON okanye iXML kube lula kwaye kube lula.

Nangona kungekho mfuneko zangaphambili zokusebenzisa iKarate, kuyanceda ukuba unolwazi olusisiseko lwe-HTTP, i-JSON, i-XML, i-JsonPath kunye ne-XPath kunye neJavaScript.


Kule posi, sijonga ezinye izinto oqhele ukuzenza kuvavanyo lwe-API oluzenzekelayo, kodwa kuqala, isikhokelo esikhawulezayo sokumisela imeko yeKarate.

Maven

Ukuba usebenzisa i-Maven, ufuna ezi zinto zimbini zilandelayo


com.intuit.karate
karate-apache
0.6.0
test
com.intuit.karate
karate-junit4
0.6.0
test

Mncinci

Ngenye indlela, ukuba usebenzisa iGradle, uyayidinga

testCompile 'com.intuit.karate:karate-junit4:0.6.0' testCompile 'com.intuit.karate:karate-apache:0.6.0'

Ulwakhiwo lweFolda

Iskripthi sovavanyo lweKarate sinefayile yolwandiso .feature Ngumgangatho olandelwa yiCucumber. Ukhululekile ukucwangcisa iifayile zakho usebenzisa iindibano zephakheji yeJava rhoqo.


Isiko leMaven kukuba neefayile zemithombo engeyiyo yeJava eyahlukileyo src/test/resources isakhiwo sefolda - kodwa abadali bezixhobo zeKarate bancomela ukuba ugcine ecaleni nge *.java iifayile.

Njenge Cucumber, kufuneka ubene 'Imbaleki' yeklasi eqhuba iifayile (ze) zomsebenzi. Ngokungafaniyo neKhukhamba, nangona kunjalo, akukho zinkcazo zinyathelo! Kwaye lo ngumlingo weKarate.

Ukuze usebenzise udidi lweTestRunner.java ukwenza ifayile yefayile, kufuneka ube necandelo lokwakha kwifayile ye-pom.xml.



4.0.0
Tutorials
Karate
1.0-SNAPSHOT




com.intuit.karate

karate-apache

0.6.0.4





com.intuit.karate

karate-junit4

0.6.0.4










src/test/java






**/*.java








Kwaye iklasi yakho yeTestRunner.java ingabonakala ngathi

package com.tutorials.karate; import com.intuit.karate.junit4.Karate; import org.junit.runner.RunWith; @RunWith(Karate.class) public class TestRunner { }

Uvavanyo lwe-API oluzenzekelayo ngeKarate

Masithi uvavanya i-API (https://some-api.com/api/users) ebuyisela uluhlu lwabasebenzisi kwifomathi yeJSON

[
{
'id': 1,
'name': 'FirstUser',
'password': 'User1Pass'
},
{
'id': 2,
'name': 'SecondUser',
'password': 'User2Pass'
} ]

Ifayile yakho yeKarate iya kubonakala ngathi:

Feature: Test User API
Scenario: Fetch all users
Given url 'https://some-api.com/api/users'
When method GET
Then status 200
And assert response.length == 2
And match response[0].name == 'FirstUser'

Kwaye yiyo loo nto-imfutshane kwaye inqaku kwaye okona kubaluleke kakhulu, akukho khowudi!


IKarate iza neseti etyebileyo yeempawu eziluncedo ezikwenza ukuba uvavanye i-API yokuZenzekelayo ngokulula nangokukhawuleza. Kwizithuba ezizayo, sijonga ngakumbi kwesi sixhobo simangalisayo kwaye sinika imizekelo yendlela yokwenza nantoni na oyifunayo xa kuvavanywa ii-API!