Course Name: Software Engineering as a Services
Course Code : SENG-361
MODULE-8
Prof. Gufran Ahmad Ansari
DEPARTMENT OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING
UNIVERSITY OF HAIL
CONTENTS MODULE- 8
ASD Testing: Test-Driven Development
FIRST, TDD, and Red–Green–Refactor ; Anatomy of a Test Case:
Arrange, Act, Assert ; Isolating Code: Doubles and Seams ;
Stubbing the Internet ; CHIPS: Intro to RSpec on Rails ; Fixtures
and Factories ; Coverage Concepts and Types of Tests ; Other
Testing Approaches and Terminology ; CHIPS: The Acceptance
Test/Unit Test Cycle
DEPARTMENT OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING
UNIVERSITY OF HAIL
ASD Testing: Test-Driven Development
ASD Testing refers to Agile Software Development Testing, which
is a set of practices and principles tailored to the Agile
methodology of software development. Agile emphasizes iterative
development, customer collaboration, and adaptability to change.
Here are the key aspects of ASD Testing in brief:
1. Continuous Integration and Continuous Testing: Regularly
integrating code changes and continuously testing them to catch
issues early.
2. Automated Testing: Heavy reliance on automated tests to
ensure rapid feedback on code quality. This includes unit tests,
integration tests, and end-to-end tests.
DEPARTMENT OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING
UNIVERSITY OF HAIL
[Link]-Driven Development (TDD): Writing tests before code to
ensure that the code meets the desired specifications from the
outset.
4. Behavior-Driven Development (BDD): Extending TDD by writing
tests in a way that describes the behavior of the application in
business-readable language.
5. Exploratory Testing: Allowing testers to explore the application
dynamically, understanding its behavior, and identifying unexpected
issues.
6. Collaborative Testing: Encouraging collaboration among
developers, testers, and business stakeholders to ensure that the
application meets user needs.
7. Short Feedback Loops: Ensuring quick feedback from tests to
developers to allow rapid fixing of issues.
DEPARTMENT OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING
UNIVERSITY OF HAIL
Test-Driven Development (TDD)
Test-Driven Development (TDD) is a software development
approach where tests are written before the code they are
intended to validate. This ensures that the codebase is
continuously tested, improving reliability and reducing the
likelihood of bugs. Let's break down the key components
mentioned:
Test-Driven Development (TDD)
1-FIRST Principles
The FIRST principles ensure that tests are effective and
maintainable:
• Fast: Tests should run quickly to encourage frequent execution.
• Independent: Tests should not depend on each other to prevent
cascading failures.
DEPARTMENT OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING
UNIVERSITY OF HAIL
• Repeatable: Tests should produce the same results every
time they run.
• Self-Validating: Tests should have a clear pass/fail outcome
without manual inspection.
• Timely: Tests should be written at the right time, ideally
before the code they test.
2-Red–Green–Refactor Cycle
The Red–Green–Refactor cycle is the core of TDD:
• Red: Write a failing test for the new functionality (since the
functionality doesn’t exist yet).
• Green: Write the minimum amount of code required to make
the test pass.
• Refactor: Clean up the code while ensuring the test still
passes.
DEPARTMENT OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING
UNIVERSITY OF HAIL
Anatomy of a Test Case
A typical test case follows the Arrange, Act, Assert pattern:
Arrange: Set up the necessary preconditions and inputs.
Act: Execute the code under test.
Assert: Verify that the output or the state of the system is as
expected.
DEPARTMENT OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING
UNIVERSITY OF HAIL
Isolating Code: Doubles and Seams
To test code in isolation, developers use test doubles and seams.
1. Test Doubles
Test doubles are objects that replace real components during
testing:
• Mocks: Objects that register calls they receive so they can
assert against expected calls.
• Stubs: Provide predefined responses to method calls.
• Fakes: Simpler implementations that work in-memory or in a
simplified way.
• Spies: Similar to mocks but record information about
interactions.
• Dummies: Passed around but never actually used.
DEPARTMENT OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING
UNIVERSITY OF HAIL
2. Seams
Seams are places in the code where you can change behavior
without modifying it. They are used to isolate the code under test:
• Constructor Injection: Pass dependencies via constructors.
• Setter Injection: Pass dependencies via setters.
• Interface Segregation: Use interfaces to define contracts and
inject implementations.
DEPARTMENT OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING
UNIVERSITY OF HAIL
Stubbing the Internet
When testing code that interacts with external services (e.g., web
APIs), you use stubs or mocks to simulate the internet:
1. HTTP Stubs: Tools like ‘nock’ in [Link] or ‘webmock’ in Ruby
can intercept HTTP requests and return predefined responses.
2. Service Virtualization: Mimics the behavior of real services,
enabling comprehensive integration testing without depending on
the actual services.
DEPARTMENT OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING
UNIVERSITY OF HAIL
Example of TDD in Action
Here’s a simple example in Python using ‘unittest’:
import unittest
# Red: Write a failing test
class TestAdder([Link]):
def test_add_two_numbers(self):
[Link](add(1, 2), 3)
# Initially, the add function does not exist
# Green: Write the minimum code to pass the test
def add(a, b):
return a + b
# Refactor: Clean up the code if necessary (in this case, it’s already
clean)
if __name__ == '__main__':
[Link]()
DEPARTMENT OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING
UNIVERSITY OF HAIL
In this example:
Red: The test fails initially because the ‘add’ function is not
defined.
Green: Define the ‘add’ function to make the test pass.
Refactor: Since the function is already simple, no refactoring is
needed.
“By following TDD and these principles, developers can create
more reliable, maintainable, and robust code”
DEPARTMENT OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING
UNIVERSITY OF HAIL
CHIPS: Intro to RSpec on Rails
RSpec is a popular testing framework for Ruby on Rails
applications, designed to provide a rich and expressive syntax for
writing tests.
1-Intro to RSpec on Rails:
• RSpec Syntax: It uses a domain-specific language to describe
the behavior of the application.
• Setting Up: Add RSpec to your Rails application by including it
in your Gemfile and running bundle install.
DEPARTMENT OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING
UNIVERSITY OF HAIL
• Basic Structure:
describe 'A feature or functionality' do
context 'under certain conditions' do
it 'does something expected' do
expect(actual).to eq(expected)
end
end
end
DEPARTMENT OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING
UNIVERSITY OF HAIL
Fixtures and Factories
Fixtures and factories are tools to set up test data.
1-Fixtures:
• Definition: Predefined data used to populate the database
with known values before tests run.
• Usage: Rails includes fixtures by default in the test/fixtures
directory.
Example:
one:
name: MyString
description: MyText
DEPARTMENT OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING
UNIVERSITY OF HAIL
Factories:
• Definition: Provide a flexible way to create test data using Factory
Bot (formerly Factory Girl).
• Usage: Define factories in spec/factories.
Example:
DEPARTMENT OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING
UNIVERSITY OF HAIL
Coverage Concepts and Types of Tests
Test Coverage measures how much of your code is tested.
1. Code Coverage:
• Definition: Percentage of code executed during tests.
• Tools: SimpleCov in Ruby can be used to measure coverage.
• [Link] of Tests:
• Unit Tests: Test individual methods or functions.
• Integration Tests: Test multiple components working together.
• Functional Tests: Test specific functionality, usually at the
controller level.
• System Tests: Test the application from the user's perspective,
interacting with the UI.
DEPARTMENT OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING
UNIVERSITY OF HAIL
Other Testing Approaches and Terminology
1-Behavior-Driven Development (BDD):
• Definition: Extends TDD by writing tests in a way that
describes the behavior of the application.
• Tools: RSpec is often used for BDD in Ruby.
2-Acceptance Testing:
• Definition: Ensures the application meets the business
requirements and behaves as expected.
• Tools: Capybara is used with RSpec for acceptance testing in
Rails.
3-Mocking and Stubbing:
• Mocking: Creating objects that simulate the behavior of real
objects.
• Stubbing: Providing canned responses to method calls in
tests.
DEPARTMENT OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING
UNIVERSITY OF HAIL
CHIPS: The Acceptance Test/Unit Test Cycle
1-Acceptance Test:
• Definition: High-level tests that verify the application’s features and
behaviors from the user's perspective.
• Process: Write acceptance tests first to define what the system
should do.
2-Unit Test:
• Definition: Low-level tests that verify individual components or
methods.
• Process: Write unit tests to ensure that each component behaves
correctly in isolation.
DEPARTMENT OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING
UNIVERSITY OF HAIL
3-Cycle:
Step 1: Write an acceptance test that describes a feature.
Step 2: Run the acceptance test and watch it fail because the feature
isn't implemented.
Step 3: Write unit tests for the individual components required to
implement the feature.
Step 4: Implement the components and make the unit tests pass.
Step 5: Run the acceptance test again and make it pass by
integrating the components.
Step 6: Refactor as necessary to improve the code while ensuring all
tests pass.
DEPARTMENT OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING
UNIVERSITY OF HAIL
THANK YOU
DEPARTMENT OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING
UNIVERSITY OF HAIL