Free MuleSoft CONNECT Keynote & Expo Pass Available!

Register now+
Nav

To Configure and Write BAT Tests

  1. Create a directory for the configuration YAML, for example /api-monitoring and a subdirectory for the tests, for example /tests.

  2. Create a minimal bat.yaml that is the manifest file for testing. For example, the bat.yaml for this procedure lists two test files:

    files:
    
     - file: ./tests/step.0.dwl
     - file: ./tests/step.1.dwl
    
    suite:
      name: "hello world test suite"
  3. Save the bat.yaml at the top level of your testing directory /api-monitoring.

  4. Create a test file. For example:

    // Functional test for http://apimon.cloudhub.io
    
    import * from bat::BDD
    import * from bat::Assertions
    ---
    describe `api-mon` in [
    
      // First, get a list of users
      GET `http://apimon.cloudhub.io/users` with {
    
      } assert [
        $.response.status mustEqual 200
      ]
    
    ]

    The GET function specifies the URL of the deployed proxy you created earlier in the workflow. For example, on CloudHub, you obtain the URL from API Manager:

    bat api mgr proxy url

  5. Save the test file in the /api-monitoring/tests subdirectory as step.0.dwl.

  6. Create another test file and save it as step.1.dwl.

    // Functional test for http://apimon.cloudhub.io
    
    import * from bat::BDD
    import * from bat::Assertions
    ---
    describe `api-mon` in [
      it `assert that there are 10 users` in [
        GET `http://apimon.cloudhub.io/users` with {} assert [
          $.response.status mustEqual 200, //< ---- ASSERTION
          $.response.mime mustEqual "application/json", //< ---- ASSERTION
          log($.response.body match{case list is Array -> sizeOf(list) mustEqual 10
            else -> false}) // < ---- ASSERTION
        ]
      ]
    ]

    This test asserts that 10 is the size of the users list array returned by the API.

In this topic: