SlideShare uma empresa Scribd logo
1 de 19
Baixar para ler offline
MedTech
Dr. Lilia SFAXI
www.liliasfaxi.wix.com/liliasfaxi
Chp6- Testing Angular
Unit Testing and End-to-end Testing
1
MedTech – Mediterranean Institute of Technology
CS-Web and Mobile Development
MedTech
MedTech
Testing
• Writing tests is intended to explore and confirm the behaviour of the
application
• Testing does the following:
• Guards against changes that break existing code (“regressions”).
• Clarifies what the code does both when used as intended and when faced
with deviant conditions.
• Reveals mistakes in design and implementation.
• Tests shine a harsh light on the code from many angles.
• When a part of the application seems hard to test, the root cause is
often a design flaw, something to cure now rather than later when it
becomes expensive to fix.
2
Testing Angular
MedTech
(Some) Types of Testing
• Unit Tests
• Smallest possible unit of testing
• Cover one small unit and don’t bother about how multiple units work together.
• Fast, reliable and point in the exact direction of the bug.
• Integration Tests
• Cover multiple units.
• Point out important bugs that would show up when multiple components run
together in the application.
• End-to-end Tests
• Simulate a production like environment.
• It’s as simple as testing your application from start to finish – just like a user.
• We can run end to end tests on multiple browsers and find bugs that emerge in
certain environments / browsers – such as… Internet Explorer.
3
Testing Angular
MedTech
Tools and Technologies
• You can write and run Angular tests with a variety of tools and technologies
• Jasmine
• Behaviour-driven development framework for testing JavaScript code.
• Provides everything needed to write basic tests.
• Angular testing utilities
• Create a test environment for the Angular application code under test.
• Used to condition and control parts of the application as they interact within the Angular
environment.
• Karma
• Ideal for writing and running unit tests while developing the application.
• Can be an integral part of the project's development and continuous integration processes.
• Protractor
• Run end-to-end (e2e) tests to explore the application as users experience it.
• One process runs the real application and a second process runs protractor tests that simulate
user behaviour and assert that the application responds in the browser as expected.
4
Testing Angular
MedTech
Testing Recommandations
• Put unit test spec files in the same folder as the application source
code files they test
• Such tests are easy to find.
• You see at a glance if a part of your application lacks tests.
• Nearby tests can reveal how a part works in context.
• When you move the source (inevitable), you remember to move the test.
• When you rename the source file (inevitable), you remember to rename the
test file.
• Application integration or end-to-end specs should be defined in a
separate test folder
• As they test the interactions of multiple parts spread across folders and
modules, they don't really belong to any part in particular
5
Testing Angular
MedTech
UNIT TESTING WITH JASMINE AND KARMA
6
Testing Angular
MedTech
Unit Testing with Jasmine
• Behaviour-driven development framework for testing JavaScript code
• Does not depend on any other JavaScript frameworks
• Does not require a DOM
• Jasmine tests are a set of Test Suites each composed of a set of Test
Specs
• Call describe to define a test suite, and it to define a spec
• A spec contains one or more expectations that test the state of the code
• A spec with all true expectations is a passing spec.
• A spec with one or more false expectations is a failing spec.
• Provides the global beforeEach, afterEach, beforeAll, and afterAll
functions
• Help a test suite DRY (Don't Repeat Yourself ) up any duplicated setup
7
Testing Angular
MedTech
Unit Testing with Jasmine
8
Testing Angular
describe("A spec using beforeEach and afterEach", function() {
var foo = 0;
beforeEach(function() {
foo += 1;
});
afterEach(function() {
foo = 0;
});
it("is just a function, so it can contain any code", function() {
expect(foo).toEqual(1);
});
it("can have more than one expectation", function() {
expect(foo).toEqual(1);
expect(true).toEqual(true);
});
});
MedTech
Karma Test Runner
• Karma provides a suitable testing environment to any web developer
• browsers do not have natively a concept of loading tests files, running
them, and reporting results.
• Karma:
1.Starts a small web server to serve "client-side" javascript files to be tested
2.Serves the "client-side" javascript files with the tests (or Specs, as they're often
called)
3.Serves a custom web page that will run javascript code for the tests
4.Starts a browser to load this page
5.Reports the results of the test to the server
6.Reports the results to text files, console, etc...
9
Testing Angular
MedTech
Angular Testing Utilities
• Isolated unit tests
• Examine an instance of a class all by itself without any dependencies on
Angular or any injected values
• The tester creates a test instance of the class with new, supplying test
doubles for the constructor parameters as needed, and then probes the test
instance API surface.
• Should be written for pipes and services
• Angular Testing Utilities
• Helper functions from @angular/core/testing
• Used for components
• Contrary to isolated unit tests, they can show how components interact with
Angular, and how they interact with their own templates
10
Testing Angular
MedTech
Writing Tests
• In order to write and run unit tests:
• Create a spec file in the same folder : usually, they have the same name as the
file they are testing, with the .spec.ts extension
• Run with karma using: npm test
• Focus on the console output to see the result, that should look like this:
• [0] is the compiler output, [1] is Karma output
11
Testing Angular
MedTech
Testing a Component
• TestBed
• Most important Angular testing utility
• Creates an Angular testing module (@NgModule class) that you configure
using configureTestingModule to add importas, dependencies…
• Enables the framework to detach the tested component from its own
application module and re-attach it to the test module
• TestBed.createComponent
• Creates an instance of the component under test
• Closes the TestBed to any further configuration
• ComponentFixture
• A handle on the test environment surrounding the created component
• Provides access to the component instance itself
12
Testing Angular
MedTech
Testing a Component
• DebugElement
• Handle on the component's DOM element
• Used to query for any HTML element of the component's template
• By
• Angular testing utility that produces useful predicates
• By.css produces a standard CSS selector
• nativeElement
• Returns the queried DOM element from the DebugElement
• fixture.detectChanges
• Detects changes of the component code to trigger data bindings and propagation
• createComponent does not automatically trigger change detection, you have to
do it manually
13
Testing Angular
MedTech
Testing a Component with External Template
• TestBed.createComponent is a synchronous method, whereas the
Angular template compiler reads the external files from the system
asynchronously
• The test setup for the component must give the Angular template compiler
time to read the files
• A first beforeEach must handle asynchronous compilation (use async())
• Call the asynchronous compileComponents method to compile all the components of the
testing module
• A synchronous beforeEach containing the remaining setup steps follows the
asynchronous beforeEach.
14
Testing Angular
// async beforeEach
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ BannerComponent ], // declare the test component
})
.compileComponents(); // compile template and css
}));
MedTech
Testing a Component with Dependency
• If a component has a service dependency, it should be tested without
really calling this service
• For testing the component, a dependency must be added to the
providers, but not the real service dependency
• Provide service test doubles (also called stubs, fakes, spies or mocks)
• Get the injected service from an injector (never call the created stub)
• Proceed normally
15
Testing Angular
beforeEach(() => {
userServiceStub = {isLoggedIn: true, user: { name: 'Test User'}};
TestBed.configureTestingModule({
declarations: [ WelcomeComponent ],
providers: [ {provide: UserService, useValue: userServiceStub } ]
});
fixture = TestBed.createComponent(WelcomeComponent);
userService = TestBed.get(UserService);
de = fixture.debugElement.query(By.css('.welcome'));
el = de.nativeElement;
});
MedTech
END TO END TESTING WITH PROTRACTOR
16
Testing Angular
MedTech
End-to-End Testing with Protractor
• Protractor is a framework dedicated to end-to-end testing of Angular
applications
• It is a Node.js program that supports the Jasmine and Mocha Test frameworks
• Protractor works in conjunction with Selenium (a browser automation
framework) to provide an automated test infrastructure that can simulate a
user’s interaction with an Angular application running in a browser or mobile
device.
17
Testing Angular
MedTech
Running Protractor
• We need two things to run the tests:
• A server running our application
• An instance of a webdriver through protractor
• Use the global variable browser to navigate in the web page
• Use element and by to call an HTML element:
• callButton = element(by.tagName('button'));
• First run the pretest to update the webdriver
• When running the test, a browser will briefly appear and disappear, and
the test results will be displayed in the console
18
Testing Angular
MedTech
Dr. Lilia SFAXI
www.liliasfaxi.wix.com/liliasfaxi
References
19
• Sites
• Angular2 official documentation, https://angular.io/docs, consulted in March
2017
• Protractor, http://www.protractortest.org/#/tutorial, consulted in March
2017

Mais conteúdo relacionado

Mais procurados

Test Automation and Selenium
Test Automation and SeleniumTest Automation and Selenium
Test Automation and SeleniumKarapet Sarkisyan
 
Test Automation Framework Designs
Test Automation Framework DesignsTest Automation Framework Designs
Test Automation Framework DesignsSauce Labs
 
e2e testing with cypress
e2e testing with cypresse2e testing with cypress
e2e testing with cypressTomasz Bak
 
Introduction to Selenium | Selenium Tutorial for Beginners | Selenium Trainin...
Introduction to Selenium | Selenium Tutorial for Beginners | Selenium Trainin...Introduction to Selenium | Selenium Tutorial for Beginners | Selenium Trainin...
Introduction to Selenium | Selenium Tutorial for Beginners | Selenium Trainin...Edureka!
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit TestingJoe Tremblay
 
Automation test framework with cucumber – BDD
Automation test framework with cucumber – BDDAutomation test framework with cucumber – BDD
Automation test framework with cucumber – BDD123abcda
 
C++ Unit Test with Google Testing Framework
C++ Unit Test with Google Testing FrameworkC++ Unit Test with Google Testing Framework
C++ Unit Test with Google Testing FrameworkHumberto Marchezi
 
Manual testing concepts course 1
Manual testing concepts course 1Manual testing concepts course 1
Manual testing concepts course 1Raghu Kiran
 
Telerik Test studio
Telerik Test studio Telerik Test studio
Telerik Test studio Ahamad Sk
 
Introducing Swagger
Introducing SwaggerIntroducing Swagger
Introducing SwaggerTony Tam
 
Hybrid Automation Framework Development introduction
Hybrid Automation Framework Development introductionHybrid Automation Framework Development introduction
Hybrid Automation Framework Development introductionGanuka Yashantha
 
Getting Started With Cypress
Getting Started With CypressGetting Started With Cypress
Getting Started With CypressKnoldus Inc.
 

Mais procurados (20)

Introduction to Selenium Web Driver
Introduction to Selenium Web DriverIntroduction to Selenium Web Driver
Introduction to Selenium Web Driver
 
TestNG Framework
TestNG Framework TestNG Framework
TestNG Framework
 
Angular
AngularAngular
Angular
 
Test Automation and Selenium
Test Automation and SeleniumTest Automation and Selenium
Test Automation and Selenium
 
Test Automation Framework Designs
Test Automation Framework DesignsTest Automation Framework Designs
Test Automation Framework Designs
 
e2e testing with cypress
e2e testing with cypresse2e testing with cypress
e2e testing with cypress
 
Introduction to Selenium | Selenium Tutorial for Beginners | Selenium Trainin...
Introduction to Selenium | Selenium Tutorial for Beginners | Selenium Trainin...Introduction to Selenium | Selenium Tutorial for Beginners | Selenium Trainin...
Introduction to Selenium | Selenium Tutorial for Beginners | Selenium Trainin...
 
testng
testngtestng
testng
 
Postman.ppt
Postman.pptPostman.ppt
Postman.ppt
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit Testing
 
Cypress Automation
Cypress  AutomationCypress  Automation
Cypress Automation
 
Automation test framework with cucumber – BDD
Automation test framework with cucumber – BDDAutomation test framework with cucumber – BDD
Automation test framework with cucumber – BDD
 
C++ Unit Test with Google Testing Framework
C++ Unit Test with Google Testing FrameworkC++ Unit Test with Google Testing Framework
C++ Unit Test with Google Testing Framework
 
Manual testing concepts course 1
Manual testing concepts course 1Manual testing concepts course 1
Manual testing concepts course 1
 
Angular Data Binding
Angular Data BindingAngular Data Binding
Angular Data Binding
 
Telerik Test studio
Telerik Test studio Telerik Test studio
Telerik Test studio
 
Introducing Swagger
Introducing SwaggerIntroducing Swagger
Introducing Swagger
 
Hybrid Automation Framework Development introduction
Hybrid Automation Framework Development introductionHybrid Automation Framework Development introduction
Hybrid Automation Framework Development introduction
 
Getting Started With Cypress
Getting Started With CypressGetting Started With Cypress
Getting Started With Cypress
 
Selenium Concepts
Selenium ConceptsSelenium Concepts
Selenium Concepts
 

Destaque

Introduction au Web
Introduction au WebIntroduction au Web
Introduction au WebLilia Sfaxi
 
Javascript Design Patterns
Javascript Design PatternsJavascript Design Patterns
Javascript Design PatternsLilia Sfaxi
 
Client-side JavaScript
Client-side JavaScriptClient-side JavaScript
Client-side JavaScriptLilia Sfaxi
 
Server-side JS with NodeJS
Server-side JS with NodeJSServer-side JS with NodeJS
Server-side JS with NodeJSLilia Sfaxi
 
Mobile developement
Mobile developementMobile developement
Mobile developementLilia Sfaxi
 
BigData_TP5 : Neo4J
BigData_TP5 : Neo4JBigData_TP5 : Neo4J
BigData_TP5 : Neo4JLilia Sfaxi
 
BigData_TP2: Design Patterns dans Hadoop
BigData_TP2: Design Patterns dans HadoopBigData_TP2: Design Patterns dans Hadoop
BigData_TP2: Design Patterns dans HadoopLilia Sfaxi
 
BigData_TP4 : Cassandra
BigData_TP4 : CassandraBigData_TP4 : Cassandra
BigData_TP4 : CassandraLilia Sfaxi
 
BigData_TP1: Initiation à Hadoop et Map-Reduce
BigData_TP1: Initiation à Hadoop et Map-ReduceBigData_TP1: Initiation à Hadoop et Map-Reduce
BigData_TP1: Initiation à Hadoop et Map-ReduceLilia Sfaxi
 
BigData_TP3 : Spark
BigData_TP3 : SparkBigData_TP3 : Spark
BigData_TP3 : SparkLilia Sfaxi
 
BigData_Chp5: Putting it all together
BigData_Chp5: Putting it all togetherBigData_Chp5: Putting it all together
BigData_Chp5: Putting it all togetherLilia Sfaxi
 
BigData_Chp3: Data Processing
BigData_Chp3: Data ProcessingBigData_Chp3: Data Processing
BigData_Chp3: Data ProcessingLilia Sfaxi
 
BigData_Chp4: NOSQL
BigData_Chp4: NOSQLBigData_Chp4: NOSQL
BigData_Chp4: NOSQLLilia Sfaxi
 
BigData_Chp2: Hadoop & Map-Reduce
BigData_Chp2: Hadoop & Map-ReduceBigData_Chp2: Hadoop & Map-Reduce
BigData_Chp2: Hadoop & Map-ReduceLilia Sfaxi
 
BigData_Chp1: Introduction à la Big Data
BigData_Chp1: Introduction à la Big DataBigData_Chp1: Introduction à la Big Data
BigData_Chp1: Introduction à la Big DataLilia Sfaxi
 

Destaque (16)

Introduction au Web
Introduction au WebIntroduction au Web
Introduction au Web
 
Javascript Design Patterns
Javascript Design PatternsJavascript Design Patterns
Javascript Design Patterns
 
Client-side JavaScript
Client-side JavaScriptClient-side JavaScript
Client-side JavaScript
 
Core JavaScript
Core JavaScriptCore JavaScript
Core JavaScript
 
Server-side JS with NodeJS
Server-side JS with NodeJSServer-side JS with NodeJS
Server-side JS with NodeJS
 
Mobile developement
Mobile developementMobile developement
Mobile developement
 
BigData_TP5 : Neo4J
BigData_TP5 : Neo4JBigData_TP5 : Neo4J
BigData_TP5 : Neo4J
 
BigData_TP2: Design Patterns dans Hadoop
BigData_TP2: Design Patterns dans HadoopBigData_TP2: Design Patterns dans Hadoop
BigData_TP2: Design Patterns dans Hadoop
 
BigData_TP4 : Cassandra
BigData_TP4 : CassandraBigData_TP4 : Cassandra
BigData_TP4 : Cassandra
 
BigData_TP1: Initiation à Hadoop et Map-Reduce
BigData_TP1: Initiation à Hadoop et Map-ReduceBigData_TP1: Initiation à Hadoop et Map-Reduce
BigData_TP1: Initiation à Hadoop et Map-Reduce
 
BigData_TP3 : Spark
BigData_TP3 : SparkBigData_TP3 : Spark
BigData_TP3 : Spark
 
BigData_Chp5: Putting it all together
BigData_Chp5: Putting it all togetherBigData_Chp5: Putting it all together
BigData_Chp5: Putting it all together
 
BigData_Chp3: Data Processing
BigData_Chp3: Data ProcessingBigData_Chp3: Data Processing
BigData_Chp3: Data Processing
 
BigData_Chp4: NOSQL
BigData_Chp4: NOSQLBigData_Chp4: NOSQL
BigData_Chp4: NOSQL
 
BigData_Chp2: Hadoop & Map-Reduce
BigData_Chp2: Hadoop & Map-ReduceBigData_Chp2: Hadoop & Map-Reduce
BigData_Chp2: Hadoop & Map-Reduce
 
BigData_Chp1: Introduction à la Big Data
BigData_Chp1: Introduction à la Big DataBigData_Chp1: Introduction à la Big Data
BigData_Chp1: Introduction à la Big Data
 

Semelhante a Testing Angular

Tools for Software Testing
Tools for Software TestingTools for Software Testing
Tools for Software TestingMohammed Moishin
 
Agile Software Testing the Agilogy Way
Agile Software Testing the Agilogy WayAgile Software Testing the Agilogy Way
Agile Software Testing the Agilogy WayJordi Pradel
 
Angular Unit testing.pptx
Angular Unit testing.pptxAngular Unit testing.pptx
Angular Unit testing.pptxRiyaBangera
 
Into The Box 2018 | Assert control over your legacy applications
Into The Box 2018 | Assert control over your legacy applicationsInto The Box 2018 | Assert control over your legacy applications
Into The Box 2018 | Assert control over your legacy applicationsOrtus Solutions, Corp
 
Beginners overview of automated testing with Rspec
Beginners overview of automated testing with RspecBeginners overview of automated testing with Rspec
Beginners overview of automated testing with Rspecjeffrey1ross
 
An Introduction to Unit Test Using NUnit
An Introduction to Unit Test Using NUnitAn Introduction to Unit Test Using NUnit
An Introduction to Unit Test Using NUnitweili_at_slideshare
 
Automated testing overview
Automated testing overviewAutomated testing overview
Automated testing overviewAlex Pop
 
Unit testing and mocking in Python - PyCon 2018 - Kenya
Unit testing and mocking in Python - PyCon 2018 - KenyaUnit testing and mocking in Python - PyCon 2018 - Kenya
Unit testing and mocking in Python - PyCon 2018 - KenyaErick M'bwana
 
Microsoft Fakes, Unit Testing the (almost) Untestable Code
Microsoft Fakes, Unit Testing the (almost) Untestable CodeMicrosoft Fakes, Unit Testing the (almost) Untestable Code
Microsoft Fakes, Unit Testing the (almost) Untestable CodeAleksandar Bozinovski
 
Laravel Load Testing: Strategies and Tools
Laravel Load Testing: Strategies and ToolsLaravel Load Testing: Strategies and Tools
Laravel Load Testing: Strategies and ToolsMuhammad Shehata
 
Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...
Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...
Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...DicodingEvent
 

Semelhante a Testing Angular (20)

Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Tools for Software Testing
Tools for Software TestingTools for Software Testing
Tools for Software Testing
 
Agile Software Testing the Agilogy Way
Agile Software Testing the Agilogy WayAgile Software Testing the Agilogy Way
Agile Software Testing the Agilogy Way
 
Angular Unit testing.pptx
Angular Unit testing.pptxAngular Unit testing.pptx
Angular Unit testing.pptx
 
Into The Box 2018 | Assert control over your legacy applications
Into The Box 2018 | Assert control over your legacy applicationsInto The Box 2018 | Assert control over your legacy applications
Into The Box 2018 | Assert control over your legacy applications
 
Ch11lect1 ud
Ch11lect1 udCh11lect1 ud
Ch11lect1 ud
 
Unit tests and TDD
Unit tests and TDDUnit tests and TDD
Unit tests and TDD
 
Unit testing
Unit testingUnit testing
Unit testing
 
Beginners overview of automated testing with Rspec
Beginners overview of automated testing with RspecBeginners overview of automated testing with Rspec
Beginners overview of automated testing with Rspec
 
An Introduction to Unit Test Using NUnit
An Introduction to Unit Test Using NUnitAn Introduction to Unit Test Using NUnit
An Introduction to Unit Test Using NUnit
 
Unit Testing in Swift
Unit Testing in SwiftUnit Testing in Swift
Unit Testing in Swift
 
Automated testing overview
Automated testing overviewAutomated testing overview
Automated testing overview
 
Unit testing and mocking in Python - PyCon 2018 - Kenya
Unit testing and mocking in Python - PyCon 2018 - KenyaUnit testing and mocking in Python - PyCon 2018 - Kenya
Unit testing and mocking in Python - PyCon 2018 - Kenya
 
Microsoft Fakes, Unit Testing the (almost) Untestable Code
Microsoft Fakes, Unit Testing the (almost) Untestable CodeMicrosoft Fakes, Unit Testing the (almost) Untestable Code
Microsoft Fakes, Unit Testing the (almost) Untestable Code
 
Unit Tests with Microsoft Fakes
Unit Tests with Microsoft FakesUnit Tests with Microsoft Fakes
Unit Tests with Microsoft Fakes
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Laravel Load Testing: Strategies and Tools
Laravel Load Testing: Strategies and ToolsLaravel Load Testing: Strategies and Tools
Laravel Load Testing: Strategies and Tools
 
Unit Tesing in iOS
Unit Tesing in iOSUnit Tesing in iOS
Unit Tesing in iOS
 
Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...
Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...
Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...
 
Test automation proposal
Test automation proposalTest automation proposal
Test automation proposal
 

Mais de Lilia Sfaxi

chp1-Intro à l'urbanisation des SI.pdf
chp1-Intro à l'urbanisation des SI.pdfchp1-Intro à l'urbanisation des SI.pdf
chp1-Intro à l'urbanisation des SI.pdfLilia Sfaxi
 
Plan d'études_INSAT_2022_2023.pdf
Plan d'études_INSAT_2022_2023.pdfPlan d'études_INSAT_2022_2023.pdf
Plan d'études_INSAT_2022_2023.pdfLilia Sfaxi
 
Lab1-DB-Cassandra
Lab1-DB-CassandraLab1-DB-Cassandra
Lab1-DB-CassandraLilia Sfaxi
 
TP2-UML-Correction
TP2-UML-CorrectionTP2-UML-Correction
TP2-UML-CorrectionLilia Sfaxi
 
TP1-UML-Correction
TP1-UML-CorrectionTP1-UML-Correction
TP1-UML-CorrectionLilia Sfaxi
 
TP0-UML-Correction
TP0-UML-CorrectionTP0-UML-Correction
TP0-UML-CorrectionLilia Sfaxi
 
TD4-UML-Correction
TD4-UML-CorrectionTD4-UML-Correction
TD4-UML-CorrectionLilia Sfaxi
 
TD3-UML-Séquences
TD3-UML-SéquencesTD3-UML-Séquences
TD3-UML-SéquencesLilia Sfaxi
 
TD3-UML-Correction
TD3-UML-CorrectionTD3-UML-Correction
TD3-UML-CorrectionLilia Sfaxi
 
TD2 - UML - Correction
TD2 - UML - CorrectionTD2 - UML - Correction
TD2 - UML - CorrectionLilia Sfaxi
 
TD1-UML-correction
TD1-UML-correctionTD1-UML-correction
TD1-UML-correctionLilia Sfaxi
 
Android - Tp1 - installation et démarrage
Android - Tp1 -   installation et démarrageAndroid - Tp1 -   installation et démarrage
Android - Tp1 - installation et démarrageLilia Sfaxi
 
Android - Tp2 - Elements graphiques
Android - Tp2 - Elements graphiques Android - Tp2 - Elements graphiques
Android - Tp2 - Elements graphiques Lilia Sfaxi
 
Android - Tp3 - intents
Android - Tp3 -  intentsAndroid - Tp3 -  intents
Android - Tp3 - intentsLilia Sfaxi
 
Android - TPBonus - web services
Android - TPBonus - web servicesAndroid - TPBonus - web services
Android - TPBonus - web servicesLilia Sfaxi
 
Android - Tp4 - graphiques avancés
Android - Tp4 - graphiques avancésAndroid - Tp4 - graphiques avancés
Android - Tp4 - graphiques avancésLilia Sfaxi
 

Mais de Lilia Sfaxi (20)

chp1-Intro à l'urbanisation des SI.pdf
chp1-Intro à l'urbanisation des SI.pdfchp1-Intro à l'urbanisation des SI.pdf
chp1-Intro à l'urbanisation des SI.pdf
 
Plan d'études_INSAT_2022_2023.pdf
Plan d'études_INSAT_2022_2023.pdfPlan d'études_INSAT_2022_2023.pdf
Plan d'études_INSAT_2022_2023.pdf
 
Lab3-DB_Neo4j
Lab3-DB_Neo4jLab3-DB_Neo4j
Lab3-DB_Neo4j
 
Lab2-DB-Mongodb
Lab2-DB-MongodbLab2-DB-Mongodb
Lab2-DB-Mongodb
 
Lab1-DB-Cassandra
Lab1-DB-CassandraLab1-DB-Cassandra
Lab1-DB-Cassandra
 
TP2-UML-Correction
TP2-UML-CorrectionTP2-UML-Correction
TP2-UML-Correction
 
TP1-UML-Correction
TP1-UML-CorrectionTP1-UML-Correction
TP1-UML-Correction
 
TP0-UML-Correction
TP0-UML-CorrectionTP0-UML-Correction
TP0-UML-Correction
 
TD4-UML
TD4-UMLTD4-UML
TD4-UML
 
TD4-UML-Correction
TD4-UML-CorrectionTD4-UML-Correction
TD4-UML-Correction
 
TD3-UML-Séquences
TD3-UML-SéquencesTD3-UML-Séquences
TD3-UML-Séquences
 
TD3-UML-Correction
TD3-UML-CorrectionTD3-UML-Correction
TD3-UML-Correction
 
TD2 - UML - Correction
TD2 - UML - CorrectionTD2 - UML - Correction
TD2 - UML - Correction
 
TD1 - UML - DCU
TD1 - UML - DCUTD1 - UML - DCU
TD1 - UML - DCU
 
TD1-UML-correction
TD1-UML-correctionTD1-UML-correction
TD1-UML-correction
 
Android - Tp1 - installation et démarrage
Android - Tp1 -   installation et démarrageAndroid - Tp1 -   installation et démarrage
Android - Tp1 - installation et démarrage
 
Android - Tp2 - Elements graphiques
Android - Tp2 - Elements graphiques Android - Tp2 - Elements graphiques
Android - Tp2 - Elements graphiques
 
Android - Tp3 - intents
Android - Tp3 -  intentsAndroid - Tp3 -  intents
Android - Tp3 - intents
 
Android - TPBonus - web services
Android - TPBonus - web servicesAndroid - TPBonus - web services
Android - TPBonus - web services
 
Android - Tp4 - graphiques avancés
Android - Tp4 - graphiques avancésAndroid - Tp4 - graphiques avancés
Android - Tp4 - graphiques avancés
 

Último

Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusZilliz
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbuapidays
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...apidays
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 

Último (20)

Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 

Testing Angular

  • 1. MedTech Dr. Lilia SFAXI www.liliasfaxi.wix.com/liliasfaxi Chp6- Testing Angular Unit Testing and End-to-end Testing 1 MedTech – Mediterranean Institute of Technology CS-Web and Mobile Development MedTech
  • 2. MedTech Testing • Writing tests is intended to explore and confirm the behaviour of the application • Testing does the following: • Guards against changes that break existing code (“regressions”). • Clarifies what the code does both when used as intended and when faced with deviant conditions. • Reveals mistakes in design and implementation. • Tests shine a harsh light on the code from many angles. • When a part of the application seems hard to test, the root cause is often a design flaw, something to cure now rather than later when it becomes expensive to fix. 2 Testing Angular
  • 3. MedTech (Some) Types of Testing • Unit Tests • Smallest possible unit of testing • Cover one small unit and don’t bother about how multiple units work together. • Fast, reliable and point in the exact direction of the bug. • Integration Tests • Cover multiple units. • Point out important bugs that would show up when multiple components run together in the application. • End-to-end Tests • Simulate a production like environment. • It’s as simple as testing your application from start to finish – just like a user. • We can run end to end tests on multiple browsers and find bugs that emerge in certain environments / browsers – such as… Internet Explorer. 3 Testing Angular
  • 4. MedTech Tools and Technologies • You can write and run Angular tests with a variety of tools and technologies • Jasmine • Behaviour-driven development framework for testing JavaScript code. • Provides everything needed to write basic tests. • Angular testing utilities • Create a test environment for the Angular application code under test. • Used to condition and control parts of the application as they interact within the Angular environment. • Karma • Ideal for writing and running unit tests while developing the application. • Can be an integral part of the project's development and continuous integration processes. • Protractor • Run end-to-end (e2e) tests to explore the application as users experience it. • One process runs the real application and a second process runs protractor tests that simulate user behaviour and assert that the application responds in the browser as expected. 4 Testing Angular
  • 5. MedTech Testing Recommandations • Put unit test spec files in the same folder as the application source code files they test • Such tests are easy to find. • You see at a glance if a part of your application lacks tests. • Nearby tests can reveal how a part works in context. • When you move the source (inevitable), you remember to move the test. • When you rename the source file (inevitable), you remember to rename the test file. • Application integration or end-to-end specs should be defined in a separate test folder • As they test the interactions of multiple parts spread across folders and modules, they don't really belong to any part in particular 5 Testing Angular
  • 6. MedTech UNIT TESTING WITH JASMINE AND KARMA 6 Testing Angular
  • 7. MedTech Unit Testing with Jasmine • Behaviour-driven development framework for testing JavaScript code • Does not depend on any other JavaScript frameworks • Does not require a DOM • Jasmine tests are a set of Test Suites each composed of a set of Test Specs • Call describe to define a test suite, and it to define a spec • A spec contains one or more expectations that test the state of the code • A spec with all true expectations is a passing spec. • A spec with one or more false expectations is a failing spec. • Provides the global beforeEach, afterEach, beforeAll, and afterAll functions • Help a test suite DRY (Don't Repeat Yourself ) up any duplicated setup 7 Testing Angular
  • 8. MedTech Unit Testing with Jasmine 8 Testing Angular describe("A spec using beforeEach and afterEach", function() { var foo = 0; beforeEach(function() { foo += 1; }); afterEach(function() { foo = 0; }); it("is just a function, so it can contain any code", function() { expect(foo).toEqual(1); }); it("can have more than one expectation", function() { expect(foo).toEqual(1); expect(true).toEqual(true); }); });
  • 9. MedTech Karma Test Runner • Karma provides a suitable testing environment to any web developer • browsers do not have natively a concept of loading tests files, running them, and reporting results. • Karma: 1.Starts a small web server to serve "client-side" javascript files to be tested 2.Serves the "client-side" javascript files with the tests (or Specs, as they're often called) 3.Serves a custom web page that will run javascript code for the tests 4.Starts a browser to load this page 5.Reports the results of the test to the server 6.Reports the results to text files, console, etc... 9 Testing Angular
  • 10. MedTech Angular Testing Utilities • Isolated unit tests • Examine an instance of a class all by itself without any dependencies on Angular or any injected values • The tester creates a test instance of the class with new, supplying test doubles for the constructor parameters as needed, and then probes the test instance API surface. • Should be written for pipes and services • Angular Testing Utilities • Helper functions from @angular/core/testing • Used for components • Contrary to isolated unit tests, they can show how components interact with Angular, and how they interact with their own templates 10 Testing Angular
  • 11. MedTech Writing Tests • In order to write and run unit tests: • Create a spec file in the same folder : usually, they have the same name as the file they are testing, with the .spec.ts extension • Run with karma using: npm test • Focus on the console output to see the result, that should look like this: • [0] is the compiler output, [1] is Karma output 11 Testing Angular
  • 12. MedTech Testing a Component • TestBed • Most important Angular testing utility • Creates an Angular testing module (@NgModule class) that you configure using configureTestingModule to add importas, dependencies… • Enables the framework to detach the tested component from its own application module and re-attach it to the test module • TestBed.createComponent • Creates an instance of the component under test • Closes the TestBed to any further configuration • ComponentFixture • A handle on the test environment surrounding the created component • Provides access to the component instance itself 12 Testing Angular
  • 13. MedTech Testing a Component • DebugElement • Handle on the component's DOM element • Used to query for any HTML element of the component's template • By • Angular testing utility that produces useful predicates • By.css produces a standard CSS selector • nativeElement • Returns the queried DOM element from the DebugElement • fixture.detectChanges • Detects changes of the component code to trigger data bindings and propagation • createComponent does not automatically trigger change detection, you have to do it manually 13 Testing Angular
  • 14. MedTech Testing a Component with External Template • TestBed.createComponent is a synchronous method, whereas the Angular template compiler reads the external files from the system asynchronously • The test setup for the component must give the Angular template compiler time to read the files • A first beforeEach must handle asynchronous compilation (use async()) • Call the asynchronous compileComponents method to compile all the components of the testing module • A synchronous beforeEach containing the remaining setup steps follows the asynchronous beforeEach. 14 Testing Angular // async beforeEach beforeEach(async(() => { TestBed.configureTestingModule({ declarations: [ BannerComponent ], // declare the test component }) .compileComponents(); // compile template and css }));
  • 15. MedTech Testing a Component with Dependency • If a component has a service dependency, it should be tested without really calling this service • For testing the component, a dependency must be added to the providers, but not the real service dependency • Provide service test doubles (also called stubs, fakes, spies or mocks) • Get the injected service from an injector (never call the created stub) • Proceed normally 15 Testing Angular beforeEach(() => { userServiceStub = {isLoggedIn: true, user: { name: 'Test User'}}; TestBed.configureTestingModule({ declarations: [ WelcomeComponent ], providers: [ {provide: UserService, useValue: userServiceStub } ] }); fixture = TestBed.createComponent(WelcomeComponent); userService = TestBed.get(UserService); de = fixture.debugElement.query(By.css('.welcome')); el = de.nativeElement; });
  • 16. MedTech END TO END TESTING WITH PROTRACTOR 16 Testing Angular
  • 17. MedTech End-to-End Testing with Protractor • Protractor is a framework dedicated to end-to-end testing of Angular applications • It is a Node.js program that supports the Jasmine and Mocha Test frameworks • Protractor works in conjunction with Selenium (a browser automation framework) to provide an automated test infrastructure that can simulate a user’s interaction with an Angular application running in a browser or mobile device. 17 Testing Angular
  • 18. MedTech Running Protractor • We need two things to run the tests: • A server running our application • An instance of a webdriver through protractor • Use the global variable browser to navigate in the web page • Use element and by to call an HTML element: • callButton = element(by.tagName('button')); • First run the pretest to update the webdriver • When running the test, a browser will briefly appear and disappear, and the test results will be displayed in the console 18 Testing Angular
  • 19. MedTech Dr. Lilia SFAXI www.liliasfaxi.wix.com/liliasfaxi References 19 • Sites • Angular2 official documentation, https://angular.io/docs, consulted in March 2017 • Protractor, http://www.protractortest.org/#/tutorial, consulted in March 2017