Software Testing Fundamentals

By Urmi Saha What is ?

1. Process of verifying and validating that a software or application is bug free

AND

2. The software meets the technical requirements as guided by it’s design and development

AND

3. The software meets the user requirements effectively and efficiently with handling all the exceptional and boundary cases Software testing is divided into 2 steps:

1. Verification

This refers to the set of tasks that ensure that software correctly implements a specific function.

“Are we building the product right?”

2. Validation

This refers to a different set of tasks that ensure that the software that has been built is traceable to customer requirements.

“Are we building the right product?” Types of software testing:

1.

The tester takes over the role of an end-user and tests the software to identify any unexpected behavior or bug without any automated tool or script.

2. Automation Testing

The tester writes scripts and uses another software to test the product. Used to re-run the test scenarios that were performed manually and repeatedly Techniques of software testing

1. Black Box Testing ● Tester doesn’t have access to the source code or the internal logical structure of the software ● Functionality of the software is not known ● This can only be done by trial and error method

2. White Box Testing ● Tester is aware of the internal workings of the product ● Tester has access to it’s source code ● Testing conducted by making sure that all internal operations are performed according to the specifications Levels of software testing

1. :

Individual units/components of a software are tested

2. Integration Testing:

Individual units are combined and tested as a group

3. System Testing:

Complete, integrated system/software is tested

4. Acceptance Testing:

Assess whether it is acceptable for delivery Unit Testing

Recap

“Unit Testing is defined as a type of software testing where individual units/ components of a software are tested”

● Performed during the development (coding) of an application ● Isolate a section of code and verify its correctness ● A unit may be an individual function or procedure ● Usually performed by the developer Unit Testing

“How is it performed?”

● A developer writes a section of code in the application just to test the function. ○ ”They would later comment out and finally remove the test code when the application is deployed.”

● Relies on mock objects being created to test sections of code that are not yet part of a complete application. ○ “Mock objects fill in for the missing parts of the program” Example of a unit test case (Django)

● Django’s unit tests use a Python standard from django.test import TestCase library module: “unittest” from myapp.models import Animal

class AnimalTestCase(TestCase): ● Here is an example which subclasses from django.test.TestCase, which is a subclass of def setUp(self): unittest.TestCase that runs each test inside a Animal.objects.create(name="lion", sound="roar") Animal.objects.create(name="cat", sound="meow") transaction to provide isolation =>

def test_animals_can_speak(self): ● When you run your tests, the default """Animals that can speak are correctly identified""" behavior of the test utility is to find all the test lion = Animal.objects.get(name="lion") cat = Animal.objects.get(name="cat") cases (that is, subclasses of self.assertEqual(lion.speak(), 'The lion says "roar"') unittest.TestCase) in any file whose name self.assertEqual(cat.speak(), 'The cat says "meow"') begins with test, automatically build a test suite out of those test cases, and run that suite. End-to-End Testing with Selenium Web App Testing Automation ● Manual tests are repeated Why automated often during development cycles for source code testing? changes. ● Once automated tests are created they can easily be repeated. What is ● Selenium automates browsers. ● It is a software testing Selenium? framework for web applications. Browser Automation ● Can be used for web-based administration tasks. 1. Python is simpler, faster and more compact than any other Why Python in . 2. It has far less verbose and Selenium? easy to use. 3. Selenium Python bindings Selenium test cases can be provides a simple API to write written in Java, Python, C#, test-cases using Selenium Ruby, , PHP, etc. WebDriver. 1. Gecko is a web browser engine 2. Gecko Driver is the link between your tests in Selenium Gecko Driver and the Firefox browser 3. Gecko Driver is an executable Drivers for web-driver file needed in one of the system path before starting automation tests Installation

1. Selenium: pip3 install selenium

2. Geckodriver for firefox: a. wget https://github.com/mozilla/geckodriver/releases/download/v0.20.1/geckodriver-v0.20.1-linux64. tar.gz b. tar -xvzf geckodriver-v0.20.1-linux64.tar.gz c. rm geckodriver-v0.20.1-linux64.tar.gz d. chmod +x geckodriver e. cp geckodriver /usr/local/bin/ 3. Write test script and run. Example of script import unittest Console Output from selenium import webdriver from selenium.webdriver.common.keys import Keys class PythonOrgSearch(unittest.TestCase): python test_python_org_search.py . def setUp(self): ------self.driver = webdriver.Firefox() ----- Ran 1 test in 15.566s def test_search_in_python_org(self): driver = self.driver driver.get("http://www.python.org") OK self.assertIn("Python", driver.title) elem = driver.find_element_by_name("q") elem.send_keys("pycon") elem.send_keys(Keys.RETURN) assert "No results found." not in driver.page_source

def tearDown(self): self.driver.close() if __name__ == "__main__": unittest.main() Examples of Selenium-WebDriver API Commands and Operations Locating UI Elements (WebElements)

1. By ID: html:

...
selenium code: element = driver.find_element_by_id("coolestWidgetEvah")

2. By Class Name: html:

Cheddar
Gouda
selenium code: cheeses = driver.find_elements_by_class_name("cheese")

3. By Name: html: selenium code: cheese = driver.find_element_by_name("cheese")

4. By XPath: html: selenium code: inputs = driver.find_elements_by_xpath("//input")

etc. Getting text values element = driver.find_element_by_id("element_id") element.text

Moving Between Windows and Frames driver.switchTo().window("windowName"); Popup Dialogs alert = driver.switch_to.alert

Navigation driver.navigate().forward(); driver.navigate().back(); Reference http://selenium-python.readthedocs.io http://selenium-python.readthedocs.io/getting-started.html https://www.seleniumhq.org/docs/03_webdriver.jsp https://docs.djangoproject.com/en/dev/topics/testing/#writing-unit-tests https://docs.djangoproject.com/en/dev/topics/testing/overview/ https://docs.djangoproject.com/en/dev/topics/testing/overview/#running-tests https://www.dev2qa.com/django-unit-test-example/ Thank You