代写代考 INFO30005 WEB IT

INFO30005 WEB IT
Improving code and app quality
RONAL SINGH

Copyright By PowCoder代写 加微信 powcoder

Why testing?
Types of tests: unit, integration, functional
Tests beyond code: functionality, security, database, etc.

WHY TESTING

WHY TESTING
• Produce high quality code
• Check that our code works — there are no bugs
• Check that our code works as expected — does what the requirements stated

TEST TYPES

T YPES OF TES TS
– Tests whether each component/function works as expected
• Integration
– Tests whether a group of related functions work together
• Functional
– Tests each functionality or feature from end-users perspective

FRAMEWORKS AND TOOLS

MANY TOOLS

MANY TOOLS
• We will use JEST to demonstrate testing • Why JEST?
– It provides combined multiple features into a single tool • Launcher; structure; assertion; mocking; stubs; spies; etc
• Have a look at the information link to get a better idea of the different tools.
https: //medium. com/welldone-software/an-overview-of- javascript-testing-7ce7298b9870

HOW TO GET STARTED?
Decide type of test
Decide the feature/function you are testing Identify test cases
– Sample input and expected output
– Normal scenario (what everything goes well – state inputs and outputs for these) and exception scenarios (what if something goes wrong – state inputs and outputs for these
• Implement your tests using JEST and run these

Basic Test Cycle
Write Tests
Repeat Tests
Run Tests –Check for fails
Update Functionality

GETTING STARTED WITH JEST

GETTING STARTED WITH JEST
• Install:
– npm install jest –save-
– –save-dev option is used because we need testing packages during development (dev) only, not for production environment

GETTING STARTED WITH JEST
• Configure npm script to run tests – Add “test” : “jest” to package.json
• Create a folder named “__tests__” (two underscores before and after test)
– JEST expects all tests to be inside this folder: __tests__

GETTING STARTED WITH JEST
• Let’s create a dummy unit test
– this test does nothing, but we create it as a “Hello World!” example – The test simply checks whether 123 == 123

GETTING STARTED WITH JEST
• Running the tests: npm test

ASSER TIONS WITH JEST
• “When you’re writing tests, you often need to check that values meet certain conditions. expect gives you access to a number of “matchers” that let you validate different things.”
https: //jestjs. io/docs/expect

FUNCTIONAL TESTING

FUNCTIONAL TESTING
• Web application is tested against functional requirements and specifications from end user’s perspective.
• One way to perform functional test is to hire people who will use the system’s functions and report on issues and success
• There are tools to automate the functional testing processes to that we have standardise protocols – let’s have a look at an example

FUNCTIONAL TESTING WITH TAIKO
• We could write test scripts that tell us how to test a feature • npm install -g taiko
https: //github. com/getgauge/taiko

FUNCTIONAL TESTING WITH TAIKO
• I saved the script under __test__ folder
taiko search_functional_test.js –observe

INTEGRATION TESTING

IN TEGR ATION TES TING WITH JEST
• Integration tests
– Tests whether a group of related functions work together
• FoodBuddy App
– Let’s test a couple of components (routes, controllers, models) when we lookup one of the foods item

IN TEGR ATION TES TING WITH JEST
• FoodBuddy App
– Let’s test a couple of components (routes, controllers, models) when we lookup one of the one food item

IN TEGR ATION TES TING WITH JEST
• FoodBuddy App: get one food • Other packages:
– supertest
• Package that allows us to send HTTP request to our app
• INSTALL: npm install supertest –save-dev
https: //www. npmjs. com/package/supertest

IN TEGR ATION TES TING WITH JEST
• FoodBuddy App: get one food

IN TEGR ATION TES TING WITH JEST
• FoodBuddy App: get one food
Test case 1:
We test the router and controller for getting one valid food, that is, a food that we know exists in our database.

IN TEGR ATION TES TING WITH JEST
• FoodBuddy App: get one food
• describe
– This function allows us to define a suite of tests. – We can define multiple tests
– Each test is defined inside the test function

IN TEGR ATION TES TING WITH JEST
• FoodBuddy App: get one food
– This function define the actual test.
– The test is defined in the callback that we supply to the test function. – Each test is implemented inside the test function

IN TEGR ATION TES TING WITH JEST
• FoodBuddy App: get one food
• I saved the test inside integration_test folder inside the __tests__ folder
– You can save it directly inside the __test__ folder as well • To run the test:
– npm test — getonefood_integration.js –forceExit
• forceExit makes sure that the test script does not hang after completing the tests

IN TEGR ATION TES TING WITH JEST
• FoodBuddy App: get one food
– Saved the test inside integration_test folder inside the __tests__ folder
– Run: npm test — getonefood_integration.js –forceExit
• forceExit makes sure that the test script does not hang after completing the tests

IN TEGR ATION TES TING WITH JEST
• FoodBuddy App: get one food
Test case 2
We are now testing get one food router and controller with an invalid food.
We expect an error message when we try to lookup an invalid food ID

IN TEGR ATION TES TING WITH JEST
• FoodBuddy App: get one food
Test 1 PASSED Test 2 FAILED
Object ID 1234 does not exist

IN TEGR ATION TES TING WITH JEST
• FoodBuddy App: get one food
The actual reason is that we did not (and will not) get anything back, the server did not send any response back or we waited for more !!!

IN TEGR ATION TES TING WITH JEST
• FoodBuddy App: get one food
Notice that we are not handling the case when a food is not found properly, that is, we are not returning any output in case these is an error. We simply print the error message in the console but nothing gets sent back to the browser, so the browser hangs!

IN TEGR ATION TES TING WITH JEST ACTIVITY
• FoodBuddy App: get one food
• Update the controller function getOneFood to render an error page with the error message that the food was not found. You can choose any error code that may be appropriate, e.g. 404 [Hint: change the catch block]

IN TEGR ATION TES TING WITH JEST
• FoodBuddy App: get one food
• What if my route is secured?
• Need to login

IN TEGR ATION TES TING WITH JEST
• FoodBuddy App: get one food

IN TEGR ATION TES TING WITH JEST
SE TUP AND TEARDOWN FUNC TIONS
• You can implement functions that run before and after tests.
• They can run once before or after ALL tests
• OR they can run before and after EACH test
• We will use the beforeALL function to login before running our tests
https: //jestjs. io/docs/setup-teardown

IN TEGR ATION TES TING WITH JEST
• We define a request agent that can maintain sessions • And we need variable to store cookie or token

IN TEGR ATION TES TING WITH JEST SETUP AND TEARDOWN
• We send a POST request to the route that handles login.
• When we get the cookie (or token) back after successful login, we will save that cookie for later sending it back.

IN TEGR ATION TES TING WI TH JES T
• Now, we use the “agent” to send the requests
• And we set the Cookie with every request – notice cookie was sent to us when we logged in. See previous slide.

IN TEGR ATION TES TING WI TH JES T
• And our two tests are now working with the secured routes!

UNIT TESTING

UNIT TESTING WITH JEST
• Unit test
– Testing of an individual function or class
– We decide inputs (if the function takes some parameters or arguments) and the outputs (what do we expect back from the function or class)
– We list inputs and outputs for both normal scenario and exception scenarios.

UNIT TESTING WITH JEST
• Now that we know where to create a test script and how to run the tests, let’s test one of functions of the FoodBuddy App
• FoodBuddy App
– Let’s test the getOneFood controller function
– The getOneFood controller function takes the Food ID (object ID) as the parameter and renders the details of the food that matches that ID.

UNIT TESTING WITH JEST
• FoodBuddy App: getOneFood controller function

UNIT TESTING WITH JEST
• FoodBuddy App: getOneFood controller function
– Notice that we want to test the functionality of the getOneFood function
in isolation, i.e.
– We don’t want to actually search the database or get the router to send requests to the controller – doing so will mean that we are doing integration testing and not unit testing
• WeneedtoMOCKthedatabaseandotherfunctions

MOCKING WITH JEST
• “…Mock functions allow you to test the links between code by erasing the actual implementation of a function…”
https: //jestjs. io/docs/mock-functions
https: //jestjs. io/docs/mock-functions

MOCKING WITH JEST
• There are numerous ways of mocking. See the following for some examples:
– YouTube video: https://www.youtube.com/watch?v=IDjF6-s1hGk
– f0046c68e53c
https: //jestjs. io/docs/mock-functions

UNIT TESTING WITH JEST
• FoodBuddy App: getOneFood controller function
We need to mock

UNIT TESTING WITH JEST

TEST DOUBLES

Wrapper for a real function
Test double types

spy object that pretends to be a real function that can record information, e.g. how many times it’s called, etc
Test double types

is a spy with the real function replaced with behavior you specify.
https: //medium. nodejs-testing-best-practices-2b98924c9347
Test double types

SPECTRUM OF WEB TESTING

WEB TESTING
https: //www. softwaretestinghelp. com/web-application-testing/

FUNC TION ALI T Y TES TING
• Web application is tested against functional requirements and specifications.
– Check all links (are there broken links)
– Test forms and reports – are they working as expected
– Check HTML and CSS – do they have syntax errors
– Test business requirements – make sure to test for exceptional scenarios and see if the error messages are appropriate

US ABILI T Y TES TING
• Tests the web application ease of use, navigation and content • Can be tested by individuals or groups

IN TERFACE TES TING
• Tests the interactions between various components – Frontend
– Backend – Database
• Are these components interacting as expected; are errors handled appropriately; is the data flow between them only what’s required (no redundancy, etc)

DATABASE TES TING
• Tests for:
– errors and how they are handled
– Efficiency – is the response time acceptable?
– Data integrity – are you duplicating details by create multiple objects with different object id?

COMPATIBILI T Y TES TING
• Tests for interoperability
– Does the app work on different browsers? – What about different operating systems?

PERFORM ANCE TES TING
• Tests for:
– Response time: do the pages load in acceptable timeframe, what
about at slow speeds?
– Does the app respond well with high traffic?
– Have you explored optimization strategies to make the app more efficient?

SECURI T Y TES TING
• Tests for:
– Unauthorized access – have you secured all routes?
– Have access be granted to people who need it?
– Are you using SSL certificates?
– Are local, session and cookies managed appropriately?

AND OTHER TYPES OF TES TING
• There are a couple of other types of tests.
• Please see the information link – it’s a very good
https: //www. softwaretestinghelp. com/web-application-testing/

Why testing?
Types of tests: unit, integration, functional
Tests beyond code: functionality, security, database, etc.

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com