Search code examples
scalatestingjunitjmeterscalatest

What could be best approach for regression suite for microservices, Can Scala be used for that?


I was asked to prepare a regression suite for testing micro-services, for that I need to: - Run scripts - Run different API's (can be multiple for scenarios to be used in next API) - Publish result in some presentable format

My own first choice was Jmeter for this, but senior resource didn't agreed and I was asked to use SCALA, while working with Scalatest , I am stuck with Futures as most of my testing revolves around calling Restful API's.

I still think Jmeter is better for what I am doing, but don't have solid base for that, can anyone suggest which one I should go for?


Solution

  • Definitely YES, I'm using scalatest for , , and my microservice.

    Have not used much JMeter, I tried it for perf testing my microservice, was bit frustrating to me, immediately gave up.

    Scalatest has been so f#*$_&g productive for me.

    Example for a very simple integration testing, (I'm using https://jsonplaceholder.typicode.com/posts/1 as my API endpoint which is online API)

    class SomeHttpFlowSpecs extends HttpFlowSpecs {
    
      feature("Getting user posts on my API server") {
    
        scenario("As a software engineer, I want to receive the user posts when I call the endpoint") {
    
          When("I send a GET request to the http endpoint")
          val response = doHttpGet("https://jsonplaceholder.typicode.com/posts/1")
    
          Then("I receive 200 response back with the user posts")
          assert(response.getStatusLine.getStatusCode == 200)
    
          val expectedJson =
            """
              |{
              |  "userId": 1,
              |  "id": 1,
              |  "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
              |  "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
              |}
            """.stripMargin
    
          assert(JSON.parseRaw(responseContent(response)) == JSON.parseRaw(expectedJson))
        }
      }
    }
    

    I wrote my own HttpFlowSpecs as a Util for making http calls and handling responses. The calls are synchronous to me unlike you are saying Future responses. I'm using very well known apache httpclient for http communication.

    class HttpFlowSpecs extends extends FeatureSpec with GivenWhenThen with BeforeAndAfterAll {
    
      def doHttpGet(endpoint: String): CloseableHttpResponse = {
        val httpRequest = new HttpGet(endpoint)
        val response = (new DefaultHttpClient).execute(httpRequest)
        response
      }
    
      def doHttpPost(endpoint: String, content: String, contentType: String = "application/json"): CloseableHttpResponse = {
        val httpRequest = new HttpPost(endpoint)
        httpRequest.setHeader("Content-type", contentType)
        httpRequest.setEntity(new StringEntity(content))
    
        val response = (new DefaultHttpClient).execute(httpRequest)
        response
      }
    
      def responseContent(response: CloseableHttpResponse): String = {
        val responseBody: String = new BufferedReader(new InputStreamReader(response.getEntity.getContent))
          .lines().collect(Collectors.joining("\n"))
    
        println("Response body = " + responseBody)
        responseBody
      }
    }
    

    Here's a very simple conifers-spec I use for my testing.

    I use pegdown - Java Markdown processor with scalatest for html reporting as below,

    pegdown

    Also, you can see all your scenarios in a very readable format, see one of my components tests in my project,

    reports

    How I run my tests

    mvn clean test
    

    Hope this is useful, let me know if any questions, would be happy to help.