Angular Unit testing
Introduction to Angular Unit testing
Angular Test Cases are automated tests written to check the functionality of an Angular application.
These tests are designed to ensure that the application is working as expected and that all components are functioning correctly.
Angular Test Cases provide a way to quickly and accurately verify that the application is working properly.
They can also be used to quickly identify any bugs or errors that may have been introduced during development.
Why do we use Angular test cases?
Using Angular test cases is important for ensuring the quality of your application's codebase.
Test cases help to detect bugs and errors in the code quickly, so that the development team can address them and fix them before they become bigger problems.
Test cases also help to make sure that new features are implemented correctly and that existing features continue to work as expected.
Finally, using test cases can help to reduce the risk of introducing new bugs into the codebase.
Unit Testing
Testing single unit of code in isolation from the rest of the code which tests behavior of a function or class
Unit testing with Jasmine and Karma for Angular Apps!
Jasmine
Jasmine is an open-source JavaScript testing framework.
JavaScript testing framework provides building blocks to write JavaScript unit test cases so each line of JavaScript statement is properly unit tested.
It is used to test any type of JavaScript application.
Jasmine is a BDD (Behavior Driven Development)
In BDD Test are written in Non-technical language so everyone can understand it easily.
BDD manly focus on the testing the behavior of code rather than implementation.
Writing test cases with Jasmine
Test cases in Jasmine are written using the ‘describe’ and ‘it’ functions. The ‘describe’ function is used to create a suite of tests, while the ‘it’ function is used to define the individual test cases.
To create a test case, first the ‘describe’ function is used to create a suite of tests and assign it a name. Then the ‘it’ function is used to define the individual test cases within the suite.
For each test case, you define the behavior you expect from the JavaScript application. You can also use assertions to test the output of the application.
Flow of Unit Test case
The Angular testing package includes two utilities called TestBed and async.
TestBed is the main Angular utility package.
The describe container contains different blocks (it, beforeEach, xit, etc.).beforeEach runs before any other block.
Karma
Karma is a testing automation tool created by the Angular JS team.
Karma is Open source tool.
Karma is a tool made on top of NodeJS to run JavaScript test cases. This is not a testing framework like Jasmine or Mocha or Chai etc.
It only allows us to run JavaScript test cases written using testing frameworks like Jasmine.
Karma allow us to execute the test cases on any browsers.
3. • Angular Test Cases are automated tests written to check the functionality of an
Angular application.
• These tests are designed to ensure that the application is working as expected
and that all components are functioning correctly.
• Angular Test Cases provide a way to quickly and accurately verify that the
application is working properly.
• They can also be used to quickly identify any bugs or errors that may have been
introduced during development.
5. • Using Angular test cases is important for ensuring the quality of your
application's codebase.
• Test cases help to detect bugs and errors in the code quickly, so that the
development team can address them and fix them before they become bigger
problems.
• Test cases also help to make sure that new features are implemented correctly
and that existing features continue to work as expected.
• Finally, using test cases can help to reduce the risk of introducing new bugs into
the codebase.
6. Unit Testing
Testing single unit of code in isolation from the rest of the code which tests
behavior of a function or class
Function
Testing Behaviour
Piece of
code
Full code
Unit Testing
8. Jasmine
• Jasmine is an open-source JavaScript testing framework.
• JavaScript testing framework provides building blocks to write JavaScript unit
test cases so each line of JavaScript statement is properly unit tested.
• It is used to test any type of JavaScript application.
• Jasmine is a BDD (Behavior Driven Development)
In BDD Test are written in Non-technical language so everyone can
understand it easily.
BDD manly focus on the testing the behavior of code rather than
implementation.
10. Test cases in Jasmine are written using the ‘describe’
and ‘it’ functions. The ‘describe’ function is used to
create a suite of tests, while the ‘it’ function is used
to define the individual test cases.
To create a test case, first the ‘describe’ function is
used to create a suite of tests and assign it a name.
Then the ‘it’ function is used to define the individual
test cases within the suite.
For each test case, you define the behavior you
expect from the JavaScript application. You can also
use assertions to test the output of the application.
11. Flow of Unit Testcase
• The Angular testing package includes two
utilities called TestBed and async.
• TestBed is the main Angular utility
package.
• The describe container contains different
blocks (it, beforeEach, xit,
etc.).beforeEach runs before any other
block.
describe
beforeEach
it()
it() it()
12.
13. Karma
• Karma is a testing automation tool created by the Angular JS team.
• Karma is Open source tool.
• Karma is a tool made on top of NodeJS to run JavaScript test cases. This is not
a testing framework like Jasmine or Mocha or Chai etc.
• It only allows us to run JavaScript test cases written using testing frameworks
like Jasmine.
• Karma allow us to execute the test cases on any browsers.
• Karma runs JavaScript test cases against real browsers through Command Line
Interface (CLI) rather than virtual browser and DOM. Since, DOM rendering
across browsers vary, so for correctness of the test report it uses real browsers.
14. Together, Jasmine and Karma provide an effective
tool for testing JavaScript applications.
With Jasmine, developers can write clear and concise
tests that are easy to understand and maintain, while
Karma provides a powerful test runner that can help
automate the testing process.
15. CREDITS: This presentation template was
created by Slidesgo, including icons by
Flaticon, infographics & images by Freepik
Thank you!!
Please keep this slide for attribution
Editor's Notes
This provides an added advantage to the users in the sense that they can add any new features without breaking any other part of their application.
The flow of how the test run is shown belowThe Angular testing package includes two utilities called TestBed and async.TestBed is the main Angular utility package. The describe container contains different blocks (it, beforeEach, xit, etc.).beforeEach runs before any other block. Other blocks do not depend on each other to run
• It can be configured with all browsers while running Karma.