Are Cucumber and Gherkin alternatives to Mocha and Chai? Can Mocha and Chai be used with Java code?

user3251882 picture user3251882 · Mar 14, 2016 · Viewed 8.5k times · Source

If someone can point out any other source on StackOverflow etc. where more info about this exists, even that will be helpful.

Answer

Dave Schweisguth picture Dave Schweisguth · Mar 18, 2016

Cucumber is an acceptance testing framework which enables tests that are readable by non-programmers. Gherkin is just the syntax in which those tests are written. The implementation of those non-programmer friendly tests can be written in multiple languages including Java, Javascript and Ruby. For testing web applications, there are tight integrations of browser drivers (Selenium, PhantomJS, etc.) with Cucumber.

Mocha is a test framework in which tests are written in Javascript. Mocha tests are not especially friendly to non-programmers. Chai is just an assertion library used with Mocha. Mocha itself doesn't provide integration with browser drivers, but there are examples on the web of how to use Javascript browser drivers from within Mocha and at least one tight integration, CodeceptJS.

So, whether Cucumber and Mocha are alternatives depends on what language you want to write your tests in and whether you care about their readability by non-programmers. If you want to write your tests in a language other than Javascript, use a Cucumber implementation in that language. If you want tests readable by non-programmers, use Cucumber. If you want to write tests in Javascript and don't care about readability by non-programmers, both Cucumber.js and Mocha (plus a browser driver) are options. There are other Javascript-friendly options than Mocha, such as Protractor for Angular applications.

Regarding Java, any acceptance testing framework that drives a browser can test a web application written in Java. If you want to write the acceptance tests themselves in Java, you probably want Cucumber-JVM. JBehave is another, older, less widely used alternative.