4

I am planning to add test suit for my web-project and some mobile api. I finalise Midje and clojure.test to test each line of code basically for (Unit Testing),

but I am unable to figure out test libraries to test end-to-end api.

what about clj-webdriver or any other better option..?

thanks and appreciation for any help.

peterh
  • 11,875
  • 18
  • 85
  • 108
piyushmandovra
  • 4,219
  • 3
  • 19
  • 30

3 Answers3

4

I use combination of clj-webdriver and Kerodon based tests:

  • I use clj-webdriver for testing scenarios that require executing JavaScript. Because these tests use a real browser, they are slow, so any scenario that doesn't need to use clj-webdriver should not use it. This is a tutorial about setting up clj-webdriver for a fresh Compojure based application.

  • I use Kerodon for all tests that don't require executing JavaScript. Kerodon doesn't start a real application server and browser so it's much faster. It's inspired by Ruby's Capybara. This is a tutorial about setting up Kerodon for a Compojure application that also includes the Kerodon API overview.

One problem with this approach is that clj-webdriver and Kerodon have a different API. Let's say you have a test that was initially written using Kerodon, since it didn't require JavaScript. If you need to execute JavaScript in the test, you will need to re-write it using clj-webdriver API. This isn't a huge problem, though, but it would be nice if there is a project that wraps both of these and would support changing driver option. Or if this feature would be part of Kerodon.

Other than 2 APIs, this approach was working well for me.

Strika
  • 634
  • 7
  • 12
2

One option is to use ring.mock to test your services through their exposed endpoints. If you are just testing API responses to various requests this is the direction I'd go.

On my current project we do use clj-webdriver to do some testing through a web browser which helps test our UI. The tests we've written have helped catch regressions though we are still trying to have the output be easy to interpret. We try not to do too much in these tests because of brittleness and difficulty in narrowing down what is broken when a test fails.

Jake McCrary
  • 1,180
  • 9
  • 8
  • hey jake but ring.mock/request just create request map, it doesn't request to server.? – piyushmandovra Mar 31 '15 at 06:15
  • my concern is should we have to write our own handler to request to server.. ? – piyushmandovra Mar 31 '15 at 06:27
  • You already have a handler that handles requests, the one that would be used when the application is actually running. From your test namespace(s) you can grab that handler and use it as a function and pass the request map to it. – Jake McCrary Apr 01 '15 at 13:47
  • 1
    dude i am using pedestal so do you have any idea what is the generic handler in pedestal app? – piyushmandovra Apr 07 '15 at 12:40
2

I wrote a Clojure library specifically for end to end tests of HTTP APIs, it's called Restpect.

This is how a simple test may look like:

(deftest create-and-delete-user
  (created (PUT "http://example.com/api/v1/users/john" {:email "john@example.com"}))
  (ok (GET "http://example.com/api/v1/users/john"))
  (ok (DELETE "http://example.com/api/v1/users/john"))
  (not-found (GET "http://example.com/api/v1/users/john")))

It also has facilities to make complex assertions on the body and other properties of the responses, as well as a custom reporter to get request and response context when a test fails.

Facundo Olano
  • 2,492
  • 2
  • 26
  • 32