Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS Testing: Protractor, Karma, Jasmine in a Yeoman App

I use this yeoman generator: https://github.com/Swiip/generator-gulp-angular

It installs three testing applications: Jasmine, Karma , Protractor According to this article (Should I be using Protractor or Karma for my end-to-end testing?), I should use: Karma for small tests of e.g. a single controller. Protactor if I want to test the whole app and simulate an user browsing through my app. According to this blog (http://andyshora.com/unit-testing-best-practices-angularjs.html) I would use Jasmine for unit testing and Karma for end-to-end integration tests.

I guess Jasmine is the language where tests are written and the other two execute the code, is that correct? Also if I never wrote a test which is more important to learn first/ to focus on?

like image 643
Andi Giga Avatar asked Jan 30 '15 10:01

Andi Giga


People also ask

What is karma Jasmine and protractor?

Jasmine and Karma are usually used together to perform Unit testing or integration testing. Protractor is an end-to-end test framework for Angular and AngularJS applications.

Does protractor use karma?

Yes, you can use karma and protractor together. Karma is used for unit testing the component you created using angular command you can test those components using karma. Protractor is used for end to end test.

Which can be used to run end to end testing karma or protractor?

When do I use which? Karma is a great tool for unit testing, and Protractor is intended for end to end or integration testing. This means that small tests for the logic of your individual controllers, directives, and services should be run using Karma.


1 Answers

Karma is a test-runner, so it runs your test. Jasmine is the framework that let you write test

In my opinion in Angularjs you :

  • must unit-test services, because your business code is there.
  • should unit-test controller, because users actions are there.
  • may unit-test custom directives (if you plan to share that directive with others, it's a must)

Protractor is made for E2E testing (tests navigation like a real user). It combines WebDriverJS with Jasmine and lets you write End-to-End tests (you simulate a real browser and taking real actions) with Jasmine syntax.

That kind of test is also really important in a web app.

You should not test everything, especially at the start of the project, those kinds of tests usually come with a high level of maintenance (i.e., when you change a screen you may have to change the test).

What I do is test the critical path and features. I made a reading app, so in my case, it was login, sign up, payment, access book, and access reader.

like image 115
Boris Charpentier Avatar answered Nov 03 '22 15:11

Boris Charpentier