1

We have a set of services that all expose certain common endpoints such as a health check, version information etc. I am trying to use karate to write smoke tests for these multiple services in a reusable way that i can just pass in the service name and endpoint and have the tests executed for each service.

basicChecks.feature

Feature: Smoke Test. verify health check and version and index are ok

  Scenario: Verify that test server health check is up and running
    Given url '#(baseUrl)'

    Given path '/health'
    When method get
    Then status 200
    And match response == "'#(name)' ok"

    Given path '/version'
    When method get
    Then status 200
    And match response contains {'#(name)'}

testServices.feature

Feature: Smoke Test for services.

  Scenario: Verify that test server health check is up and running

    * call read('basic.feature') { name: 'service1' , baseUrl : service1Url }
    * call read('basic.feature') { name: 'service2' , baseUrl : service2Url }

karate-config.js

function fn() {
  var env = karate.env; // get java system property 'karate.env'
  karate.log('karate.env system property was:', env);
  if (!env) {
    env = 'local'; // a custom 'intelligent' default
  }
  var config = { // base config JSON
    appId: 'my.app.id',
    appSecret: 'my.secret',
    service1Url: 'https://myserver/service1'
    service2Url: 'https://myserver/service2'
  };
 
  // don't waste time waiting for a connection or if servers don't respond within 5 seconds
  karate.configure('connectTimeout', 5000);
  karate.configure('readTimeout', 5000);
  return config;
}

When i run this i get an error suggesting that the baseUrl is not being picked up when passed in

20:27:22.277 karate.org.apache.http.ProtocolException: Target host is not specified, http call failed after 442 milliseconds for url: /health#(baseUrl) 20:27:22.278 cas/src/test/java/karate/smoke/basic.feature:7 When method get http call failed after 442 milliseconds for url: /health#(baseUrl) cas/src/test/java/karate/smoke/basic.feature:7

I looked at https://intuit.github.io/karate/#code-reuse--common-routines but could not figure out how to use the same tests but pass in different endpoints?

Or maybe since i am totally new to karate there is a much better way of doing this than what i have outlined?

Thank you for your time.

Edit - I am trying to test different micro services in the same environment and not trying to switch different environments etc.

Gokul
  • 237
  • 3
  • 13

1 Answers1

0

This is not the recommended approach. When you have different URL-s for different environments, you should switch environments using the approach in the documentation (setting karate.env) and NOT depend on re-use via "call" etc.

Example: https://stackoverflow.com/a/49693808/143475

And if you really want you can run suites one after the other switching the karate.env, although that is rare.

Or if you just trying "data" driven testing, there are plenty of ways, just read the docs and search Stack Overflow for Scenario Outline: https://stackoverflow.com/search?tab=newest&q=%5bkarate%5d%20Scenario%20Outline

If you are trying to do this "clever" re-use using "call" I strongly recommend that you don't and please read this for why: https://stackoverflow.com/a/54126724/143475

EDIT - I think you ran into this problem, read the docs please: https://github.com/intuit/karate#rules-for-embedded-expressions

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • These are different micro services in the same environment only. – Gokul Sep 08 '21 at 02:25
  • @Gokul I think you ran into this specific problem with embedded expressions, read this please: https://github.com/intuit/karate#rules-for-embedded-expressions – Peter Thomas Sep 08 '21 at 04:15