SlideShare a Scribd company logo
1 of 66
14/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir)
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 2
34/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir)
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 4
• Test driven development (TDD) is a software development approach.
• In TDD a test is written before writing the code.
• Once the new code passes the test, it is refactored to an acceptable
standard.
 TDD ensures that the source code is thoroughly unit tested and leads
to modularized, flexible and extensible code.
 TDD focuses on writing only the code necessary to pass tests, making
the design simple and clear.
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 5
• The test is written before testing the
functionality
• Ensures that the application is suitable for
testability.
• The functionality is implemented.
• This is referred to as "red green refactor.
• Red means fail and green shows a pass.
• These steps are repeated.
o The first goal of a programmer is to focus
on the task at hand and to pass it.
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 6
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 7
 TDD can lead to more modularized, flexible, and extensible code
 Clean code
 Leads to better design
 Better code documentation
 More productive
 Good design
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 8
 My goal is for you to see the rhythm of test-driven development:
 1. Quickly add a test
 2. Run all tests and see the new one fail
 3. Make a little change
 4. Run all tests and see them all succeed
 5. Refactor to remove duplication
 The surprises are likely to be:
 How each test can cover a small increment of functionality
 How small and ugly the changes can be to make the new tests run
 How often the tests are run
 How many teensy tiny steps make up the refactorings
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 9
1. Create class-diagram/Components-model using a class designer
2. Generate code from the diagram
3. Select a method to test
4. Generate test using Nunit, Junit, …
5. Run the test
6. The test fails
7. Complete the selected method
8. The test passes
9. Clean the code
10. Refactor the code
11. repeat
Unit testing requires that the source code is composed in
such a way that dependencies between modules can be
easily neutralized with mocks. In addition, unit testing
requires that functions are well isolated from each other
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 10
 Consider the class diagram, shown in the next slide.
 The diagram is depicted in the Enterprise Architect environment.
 Generate C# code from the class diagram.
 Make a C# console application(.sln file).
 Add the generated classes to the console application.
 Use Nunit to generate test classes and unit test.
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 11
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 12
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 13
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 14
///////////////////////////////////////////////////////////
// point.cs
// Implementation of the Class point
// Generated by Enterprise Architect
// Created on: 09-Apr-2020 5:44:14 PM
// Original author: Asus
///////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Geometry;
namespace Geometry {
public class point : GraphicalObject {
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 15
public double x;
public double y;
public point(){
}
~point(){
}
///
/// <param name="xCoord"></param>
/// <param name="yCoord"></param>
public point(double xCoord, double yCoord){
}
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 16
/// <param name="p"></param>
/// <param name="q"></param>
public static double dist(point p, point q){
return 0;
}
///
/// <param name="p"></param>
/// <param name="q"></param>
public static double distPower2(point p, point q){
return 0;
}
public void draw(){
}
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 17
public void dump(){
}
///
/// <param name="p"></param>
/// <param name="q"></param>
/// <param name="r"></param>
public static bool isCoLinear(point p, point q, point r){
return false;
}
///
/// <param name="aPoint"></param>
public Boolean isEqual(point aPoint){
return null;
}
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 18
///
/// <param name="xc"></param>
/// <param name="yc"></param>
/// <param name="angel"></param>
public void rotate(int xc, int yc, double angel){
}
///
/// <param name="x_val"></param>
/// <param name="y_val"></param>
public void Set(double x_val, double y_val){
}
}//end point
}//end namespace Geometry
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 19
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 20
// This groups geometric objects as a whole object.
class AccessModifiers
{ static void Main(string[] args)
{ point p1, p2, p3, p4;
p1 = new point(1, 1); p2 = new point(3, 1); p3 = new point(1, 4);
Triangels MyTriangel = new Triangels(p1, p2, p3);
MyTriangel.rotate(1, 2, 30);
//MyTriangel.draw();
p1.Set(1, 1); p2.Set(3, 1); p3.Set(3, 4); p4 = new point(1, 4);
Quadrilateral MyQuadrilateral = new Quadrilateral(p1, p2, p3, p4);
//MyQuadrilateral.draw();
GroupObject MyGroupObject = new GroupObject();
MyGroupObject.addObject(MyTriangel);
MyGroupObject.addObject(MyQuadrilateral);
MyGroupObject.draw();
Console.ReadLine(); }}}
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 21
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 22
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 23
 Use NUnit3 in visual studio environment to create test classes.
 If Nunit is not installed you will have to install it.
 Right click on the name of the method to be tested.
 Select “Create unit test option”
 A unit is the smallest possible part of a program that can be logically isolated
and tested.
 A method, function, procedure, or subroutine is an instance of a unit. Unit
testing is the first phase of testing.
 As an example, consider the class point, in C#, listed below:
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 24
public class point : GraphicalObject
{
private double x, y;
public point(double xCoord, double yCoord){}
public void Set(double x_val, double y_val) {}
// Computes the distance between the points P and q.
public static double dist(point p, point q) {}
public static double distPower2(point p, point q){}
// Determines whether
public static bool isCoLinear(point p, point q, point r){}
public void dump(){}
public Boolean isEqual(point aPoint){}
// Rotates the point object around the point (xc, yc) for angle.
public void rotate(int xc, int yc, double angel){}
public void draw(){}
}
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 25
 A unit is the smallest possible part of a program that can be logically isolated
and tested.
 A method, function, procedure, or subroutine is an instance of a unit. Unit
testing is the first phase of testing.
 As an example, right click on the name of the method “dist” in the class
“point”,
public static double dist(point p, point q) { … }
 Select the “create test” option as shown in the bext slide.
 If “Nunit” is not installed click on the “Get additional extensions”.
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 26
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 27
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 28
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 29
1. Test Cases are copied
into x1, y1, x2 and y2,
respectively.
2. Result is compared
with the retuen value.
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 30
The test cases show two points
and their expected distances.
Eg.
[TestCase(0,0, 1,0, Result = 1)]
P1 = (0,0) , P2 = (1,0)
dist(P1, P2) = 1
 To run tests select:
Test->Run All Tests
 To view test results:
Test->Test Explorer
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 31
 The previous test fails.
 Complete the body of the “dist” method as follows:
// Computes the distance between the points p and q
public static double dist(point p, point q)
{
double res = 0;
if (p.x == q.x) { res = Math.Abs(p.y - q.y); return res; }
if (p.y == q.y) { res = Math.Abs(p.x - q.x); return res; }
double dx = p.x - q.x; double dy = p.y - q.y;
res = Math.Sqrt(dx * dx + dy * dy);
return res;
}
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 32
 Run the tests again:
Test->Run All Tests
 To view test results:
Test->Test Explorer
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 33
 Complete the test method.
 The test fails:
[Test()]
[TestCase(0, 0, 90, 2, 0, Result = new double[2] { 0, 2 })]
[TestCase(0, 0, 90, 1, 1, Result = new double[2] { 0, 1 })]
[TestCase(0, 0, 90, 1, 0, Result = new double[2] { 0, 1 })]
public double[] rotateTest(int xc, int yc, double angel, double x, double y)
{
point p = new point(x, y);
p.rotate(xc, yc, angel);
var result = new double[2];
result[0] = p.x;
result[1] = p.y;
Console.Write(" (x = {0}), y={1}", p.x, p.y);
return result;
}
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 34
 The previous test fails.
 Complete the body of the “rotate” method as follows:
// Rotates “this” object around point (xc, yc) for angle anel1 degree
public void rotate(int xc, int yc, double angel)
{
//point ComparedWith = new point(xc, yc);
angel = angel*3.141592/180;
if (xc == x && yc == y) return;
else
{
double cosa = Math.Cos(angel); double sina = Math.Sin(angel);
double dx = x - xc; double dy = y - yc;
x = (int)Math.Round(cosa * dx - sina * dy + xc);
y = (int)Math.Round(sina * dx + cosa * dy + yc);
}
}
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 35
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 36
Write all the test cases for testing the “dist” method in a text file, testCases.txt.
0,0,0,1,1
0,0,2,0,2
0,0,3,4,5
0,0,0,0,0
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 37
namespace Geometry.Tests
{
[TestFixture()]
public class pointTests
{[Test, TestCaseSource(typeof(MyTestCases), "TestCasesFromFile")]
//[Test, TestCaseSource(typeof(MyTestCases), "TestCases")]
public double distTest(point p, point q)
{
var result = point.dist(p, q);
p.dump(); q.dump(); Console.WriteLine(" distance = {0}", result);
return result;
}
}
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 38
public class MyTestCases
{public static IEnumerable TestCasesFromFile
{ get
{ using (StreamReader file = new StreamReader(@"D:TestCases.txt"))
{ string ln;
while ((ln = file.ReadLine()) != null)
{ string[] values = ln.Split(',');
point p = new point(Convert.ToDouble(values[0]),
Convert.ToDouble(values[1]));
point q = new point(Convert.ToDouble(values[2]),
Convert.ToDouble(values[3]));
yield return new TestCaseData(p,
q).Returns(Convert.ToDouble(values[4]));
}
}
}
} } }
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 39
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 40
 Requirements:
 toRoman:
 should return the Roman numeral representation for all integers 1 to 3999.
 should always return a Roman numeral using uppercase letters.
 fromRoman:
 should take a valid Roman numeral and return the number that it represents.
 should only accept uppercase Roman numerals (i.e. it should fail when
given lowercase input).
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 41
#roman.py
"""Convert to and from Roman numerals"""
#Define exceptions
class RomanError(Exception): pass
class OutOfRangeError(RomanError): pass
class NotIntegerError(RomanError): pass
class InvalidRomanNumeralError(RomanError): pass
def toRoman(n):
"""convert integer to Roman numeral"""
pass
def fromRoman(s):
"""convert Roman numeral to integer"""
pass
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 42
Unittest: testRoman.py
"""Unit test for roman.py"""
import roman
import unittes
knownValues = ( (1, 'I'),.,.,.,..,(3999, 'MMMCMXCIX'))
class TestToRoman(unittest.TestCase):
def testToRomanGood(self):
"""toRoman should give known result with known input"""
for integer, numeral in knownValues:
result = roman.toRoman(integer)
self.assertEqual(numeral, result)
def testNonInteger(self):
"""toRoman should fail with non-integer input"""
self.assertRaises(roman.NotIntegerError, roman.toRoman, 0.5)
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 43
class TestFromRoman(unittest.TestCase):
def setup(self):
pass
def tesrdown(self):
pass
def test_knownValues(self):
"""fromRoman should give known result with known input"""
for integer, numeralin knownValues:
result = roman.fromRoman(numeral)
self.assertEqual(integer, result)
def testTooManyRepeatedNumerals(self):
"""fromRoman should fail with too many repeated numerals"""
for s in ('MMMM', 'DD', 'CCCC', 'LL', 'XXXX', 'VV', 'IIII'):
self.assertRaises(roman.InvalidRomanNumeralError, roman.fromRoman, s)
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 44
"""Convert to and from Roman numerals"""
#Define exceptions
class RomanError(Exception): pass
class OutOfRangeError(RomanError): pass
class NotIntegerError(RomanError): pass
class InvalidRomanNumeralError(RomanError): pass
#Define digit mapping
romanNumeralMap = (('M', 1000), ('CM', 900), ('D', 500),('CD',
400), ('C', 100), ('XC', 90), ('L', 50),('XL', 40), ('X', 10), ('IX', 9), ('V',
5), ('IV', 4), ('I', 1))
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 45
def toRoman(n):
"""convert integer to Roman numeral"""
if not (0 < n < 4000):
raise OutOfRangeError, "number out of range (must be 1..3999)"
if int(n) <> n:
raise NotIntegerError, "non-integers can not be converted"
result = ""
for numeral, integer in romanNumeralMap:
while n >= integer:
result += numeral
n -= integer
return result
def fromRoman(s):
"""convert Roman numeral to integer"""
pass
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 46
def fromRoman(s):
"""convert Roman numeral to integer"""
result = 0
index = 0
for numeral, integer in romanNumeralMap:
while s[index:index+len(numeral)] == numeral:
result += integer
index += len(numeral)
return result
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 47
 The nose.tools module provides a number of testing aids:
Decorators: for restricting test execution time and testing for exceptions,
Assert: methods found in unittest.TestCase (only spelled in pep08 fashion, so
assert_equal rather than assertEqual).
nose.tools.raises(*exceptions)
Test must raise one of expected exceptions to pass.
 Example use:
@raises(TypeError, ValueError)
def test_raises_type_error():
raise TypeError("This test passes")
nose.tools.assert_equal
-- Fail if the two objects are unequal
-- as determined by the ‘==’ operator.
nose.tools.assert_false
-- Fail the test if the expression is true.
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 48
The overall architecture of the payroll application software consists of three packages.
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 49
The “Guard interface” and “Time card reader” are two parallel tasks.
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 50
There is a time card reader in
the payroll department, as
well.
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 51
To assign a class to a component (In Rational-Rose environment) by:
1. Click on the component.
2. A list of all the classes will pop up.
3. Click on the name of a class,
4. Assign the class to the component.
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 52
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 53
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 54
• [1] Erdogmus, Hakan; Morisio, Torchiano. On the Effectiveness of Test-first Approach to
Programming. Proceedings of the IEEE Transactions on Software Engineering, 31(1). January 2005.
(NRC 47445). Retrieved on 2008-01-14.
• [2] George, Boby; Williams, Laurie. A Structured experiment of test-driven development. 2003.
• [3] E.M. Maximilien, L. Williams; Assessing test-development at IBM, presented at International
Conference of Software Engineering, Portland, OR, 2003.
• [4] Beck, K. Test-Driven Development by Example, Addison Wesley, 2003
http://en.wikipedia.org/wiki/Test_driven_development#Test-Driven_Development_Cycle
• [5] JMock.org, www.jmock.org, Year ?
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 55
• What?
• Software testing is the art of measuring and maintaining software quality.
• Why?
• To ensure that user expectations and requirements, business value, non-
functional requirements, such as security, reliability and recoverability,
and operational policies are all met.
• How?
• Testing strategies: Black, white, and gray box testing.*
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 56
• How?
• Testing strategies are traditionally divided into black, white, and gray box
testing.
- Black: The inside of the box (“solution implementation”) is dark. Testers focus only on
input and output, typically when performing system and user acceptance testing.
- White: The inside of the box is visible and analyzed as part of the testing.
- Gray: A combination of black and white box testing typically used to test edge cases,
which require an understanding of the internals and expected behavior.
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 57
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 58
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 59
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 60
• Microsoft Fakes is a new code isolation framework that can help you isolate code
for testing by replacing other parts of the application with stubs or shims.
• It allows you to test parts of your solution even if other parts of your app haven’t
been implemented or aren’t working yet.
• Microsoft Fakes come in two flavors:
 Stubs … replace a class with a small substitute (“stub”) that implements the
same interface.
 Shims … modify the compiled code at run time, to inject and run a substitute
(“shim”).
• Stubs are typically used for calls within your solution that you can decouple using
interfaces; shims are used for calls to referenced assemblies for which the code is
not under your control.
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 61
• Fakes come in two flavours:
• A shim modifies the compiled code of your application at run time so that
instead of making a specified method call, it runs the shim code that your test
provides. Shims can be used to replace calls to assemblies that you cannot
modify, such .NET assemblies.
• A stub replaces a class with a small substitute that implements the same
interface. To use stubs, you have to design your application so that each
component depends only on interfaces, and not on other components.
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 62
We have a class that implements something depends on System library.
using System;
namespace DemoClassLibrary
{
public class Foo
{
public long UtcNowTick()
{
return DateTime.UtcNow.Ticks;
}
}
}
So this UtcNowTick is depends on the
DateTime, and right now we want to test this
function
This is a typical scenario for us to use shim to
test because we cannot control the behaviour
of the system library.
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 63
• Add the fake to the desired library
As a result a fake assembly will be created:
after-add-fake.jpg
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 64
 So right now, we have the fake assembly for system now, and we can
control the behaviour of system namespace api as below:
Resources – TDD & refactoring
test-driven development:
A Practical Guide
Dave Astels
Prentice-Hall/Pearson Education, 2003
ISBN 0-13-101649-0
___________________________
Test-Driven Development:
By Example
Kent Beck
Addison-Wesley, 2003
ISBN 0-321-14653-0
654/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir)
Resources – TDD & refactoring
Agile Java - Crafting Code
with Test-Driven
Development
Jeff Langr
Prentice Hall 2005
ISBN 0-13-148239-4
66
4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir)

More Related Content

What's hot (20)

Introduction to c++
Introduction to c++Introduction to c++
Introduction to c++
 
Handout#11
Handout#11Handout#11
Handout#11
 
Literals,variables,datatype in C#
Literals,variables,datatype in C#Literals,variables,datatype in C#
Literals,variables,datatype in C#
 
2. introduction
2. introduction2. introduction
2. introduction
 
Handout#02
Handout#02Handout#02
Handout#02
 
Methods in C#
Methods in C#Methods in C#
Methods in C#
 
Operators and Expressions in C#
Operators and Expressions in C#Operators and Expressions in C#
Operators and Expressions in C#
 
2. introduction to compiler
2. introduction to compiler2. introduction to compiler
2. introduction to compiler
 
Assignment11
Assignment11Assignment11
Assignment11
 
C++
C++C++
C++
 
Handout#05
Handout#05Handout#05
Handout#05
 
Handout#08
Handout#08Handout#08
Handout#08
 
Handout#01
Handout#01Handout#01
Handout#01
 
Function overloading ppt
Function overloading pptFunction overloading ppt
Function overloading ppt
 
C++
C++C++
C++
 
Chapter 2 c#
Chapter 2 c#Chapter 2 c#
Chapter 2 c#
 
A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++
 
Structure and Enum in c#
Structure and Enum in c#Structure and Enum in c#
Structure and Enum in c#
 
C interview-questions-techpreparation
C interview-questions-techpreparationC interview-questions-techpreparation
C interview-questions-techpreparation
 
Handout#06
Handout#06Handout#06
Handout#06
 

Similar to 6-TDD

Linked Data in Learning Analytics Tools
Linked Data in Learning Analytics ToolsLinked Data in Learning Analytics Tools
Linked Data in Learning Analytics ToolsMathieu d'Aquin
 
TDD CrashCourse Part3: TDD Techniques
TDD CrashCourse Part3: TDD TechniquesTDD CrashCourse Part3: TDD Techniques
TDD CrashCourse Part3: TDD TechniquesDavid Rodenas
 
Data structures and algorithms lab1
Data structures and algorithms lab1Data structures and algorithms lab1
Data structures and algorithms lab1Bianca Teşilă
 
[PL] O klasycznej, programistycznej elegancji
[PL] O klasycznej, programistycznej elegancji[PL] O klasycznej, programistycznej elegancji
[PL] O klasycznej, programistycznej elegancjiJakub Marchwicki
 
Java Core | Concurrency in the Java Language and Platform | Fredrik Ohrstrom
Java Core | Concurrency in the Java Language and Platform | Fredrik OhrstromJava Core | Concurrency in the Java Language and Platform | Fredrik Ohrstrom
Java Core | Concurrency in the Java Language and Platform | Fredrik OhrstromJAX London
 
What is new in sulu 2.0
What is new in sulu 2.0What is new in sulu 2.0
What is new in sulu 2.0danrot
 
C and Data structure lab manual ECE (2).pdf
C and Data structure lab manual ECE (2).pdfC and Data structure lab manual ECE (2).pdf
C and Data structure lab manual ECE (2).pdfjanakim15
 
JCConf 2020 - New Java Features Released in 2020
JCConf 2020 - New Java Features Released in 2020JCConf 2020 - New Java Features Released in 2020
JCConf 2020 - New Java Features Released in 2020Joseph Kuo
 
Refactoring - improving the smell of your code
Refactoring - improving the smell of your codeRefactoring - improving the smell of your code
Refactoring - improving the smell of your codevmandrychenko
 
What is Pure Functional Programming, and how it can improve our application t...
What is Pure Functional Programming, and how it can improve our application t...What is Pure Functional Programming, and how it can improve our application t...
What is Pure Functional Programming, and how it can improve our application t...Luca Molteni
 
16. Java stacks and queues
16. Java stacks and queues16. Java stacks and queues
16. Java stacks and queuesIntro C# Book
 
project report in C++ programming and SQL
project report in C++ programming and SQLproject report in C++ programming and SQL
project report in C++ programming and SQLvikram mahendra
 

Similar to 6-TDD (20)

Linked Data in Learning Analytics Tools
Linked Data in Learning Analytics ToolsLinked Data in Learning Analytics Tools
Linked Data in Learning Analytics Tools
 
8-bad-smells
8-bad-smells8-bad-smells
8-bad-smells
 
TDD CrashCourse Part3: TDD Techniques
TDD CrashCourse Part3: TDD TechniquesTDD CrashCourse Part3: TDD Techniques
TDD CrashCourse Part3: TDD Techniques
 
L10
L10L10
L10
 
Data structures and algorithms lab1
Data structures and algorithms lab1Data structures and algorithms lab1
Data structures and algorithms lab1
 
Java Class Design
Java Class DesignJava Class Design
Java Class Design
 
[PL] O klasycznej, programistycznej elegancji
[PL] O klasycznej, programistycznej elegancji[PL] O klasycznej, programistycznej elegancji
[PL] O klasycznej, programistycznej elegancji
 
Java Core | Concurrency in the Java Language and Platform | Fredrik Ohrstrom
Java Core | Concurrency in the Java Language and Platform | Fredrik OhrstromJava Core | Concurrency in the Java Language and Platform | Fredrik Ohrstrom
Java Core | Concurrency in the Java Language and Platform | Fredrik Ohrstrom
 
What is new in sulu 2.0
What is new in sulu 2.0What is new in sulu 2.0
What is new in sulu 2.0
 
C and Data structure lab manual ECE (2).pdf
C and Data structure lab manual ECE (2).pdfC and Data structure lab manual ECE (2).pdf
C and Data structure lab manual ECE (2).pdf
 
JCConf 2020 - New Java Features Released in 2020
JCConf 2020 - New Java Features Released in 2020JCConf 2020 - New Java Features Released in 2020
JCConf 2020 - New Java Features Released in 2020
 
Refactoring - improving the smell of your code
Refactoring - improving the smell of your codeRefactoring - improving the smell of your code
Refactoring - improving the smell of your code
 
C++ manual Report Full
C++ manual Report FullC++ manual Report Full
C++ manual Report Full
 
SOLID Java Code
SOLID Java CodeSOLID Java Code
SOLID Java Code
 
Android best practices
Android best practicesAndroid best practices
Android best practices
 
Methods
MethodsMethods
Methods
 
What is Pure Functional Programming, and how it can improve our application t...
What is Pure Functional Programming, and how it can improve our application t...What is Pure Functional Programming, and how it can improve our application t...
What is Pure Functional Programming, and how it can improve our application t...
 
16. Java stacks and queues
16. Java stacks and queues16. Java stacks and queues
16. Java stacks and queues
 
project report in C++ programming and SQL
project report in C++ programming and SQLproject report in C++ programming and SQL
project report in C++ programming and SQL
 
JavaCro'14 - Scala and Java EE 7 Development Experiences – Peter Pilgrim
JavaCro'14 - Scala and Java EE 7 Development Experiences – Peter PilgrimJavaCro'14 - Scala and Java EE 7 Development Experiences – Peter Pilgrim
JavaCro'14 - Scala and Java EE 7 Development Experiences – Peter Pilgrim
 

More from Morteza Zakeri

Antlr part3 getting_started_in_c_sharp
Antlr part3 getting_started_in_c_sharpAntlr part3 getting_started_in_c_sharp
Antlr part3 getting_started_in_c_sharpMorteza Zakeri
 
Antlr part1 introduction
Antlr part1 introductionAntlr part1 introduction
Antlr part1 introductionMorteza Zakeri
 
Antlr part2 getting_started_in_java
Antlr part2 getting_started_in_javaAntlr part2 getting_started_in_java
Antlr part2 getting_started_in_javaMorteza Zakeri
 
2-requirements-modelling
2-requirements-modelling2-requirements-modelling
2-requirements-modellingMorteza Zakeri
 
1-requirements-elicitation
1-requirements-elicitation1-requirements-elicitation
1-requirements-elicitationMorteza Zakeri
 
Analysis of Social Phenomena Using Machine Learning Techniques: A Mixed Resea...
Analysis of Social Phenomena Using Machine Learning Techniques: A Mixed Resea...Analysis of Social Phenomena Using Machine Learning Techniques: A Mixed Resea...
Analysis of Social Phenomena Using Machine Learning Techniques: A Mixed Resea...Morteza Zakeri
 
Internet of Things: Middle-ware Platforms, Security, and Intrusion Detection
Internet of Things: Middle-ware Platforms, Security, and Intrusion DetectionInternet of Things: Middle-ware Platforms, Security, and Intrusion Detection
Internet of Things: Middle-ware Platforms, Security, and Intrusion DetectionMorteza Zakeri
 
Community Detection with Genetic Algorithm
Community Detection with Genetic AlgorithmCommunity Detection with Genetic Algorithm
Community Detection with Genetic AlgorithmMorteza Zakeri
 
SpotifyX Architectural Review
SpotifyX Architectural ReviewSpotifyX Architectural Review
SpotifyX Architectural ReviewMorteza Zakeri
 
An overview of anomaly detection techniques
An overview of anomaly detection techniquesAn overview of anomaly detection techniques
An overview of anomaly detection techniquesMorteza Zakeri
 
SQLite and object-relational mapping in Java
SQLite and object-relational mapping in JavaSQLite and object-relational mapping in Java
SQLite and object-relational mapping in JavaMorteza Zakeri
 
Apache Mesos: Architecture, Design and Code Review
Apache Mesos: Architecture, Design and Code ReviewApache Mesos: Architecture, Design and Code Review
Apache Mesos: Architecture, Design and Code ReviewMorteza Zakeri
 
یادگیری توالی به توالی با شبکه های عصبی
یادگیری توالی به توالی با شبکه های عصبییادگیری توالی به توالی با شبکه های عصبی
یادگیری توالی به توالی با شبکه های عصبیMorteza Zakeri
 
Sequence to sequence learning with neural networks
Sequence to sequence learning with neural networksSequence to sequence learning with neural networks
Sequence to sequence learning with neural networksMorteza Zakeri
 
Bridge Management System Using NoSQL Solutions
Bridge Management System Using NoSQL SolutionsBridge Management System Using NoSQL Solutions
Bridge Management System Using NoSQL SolutionsMorteza Zakeri
 
Extracting architectural model of software from source code
Extracting architectural model of software from source codeExtracting architectural model of software from source code
Extracting architectural model of software from source codeMorteza Zakeri
 
Software Fault Avoidance in Implementation
Software Fault Avoidance in ImplementationSoftware Fault Avoidance in Implementation
Software Fault Avoidance in ImplementationMorteza Zakeri
 

More from Morteza Zakeri (20)

Antlr part3 getting_started_in_c_sharp
Antlr part3 getting_started_in_c_sharpAntlr part3 getting_started_in_c_sharp
Antlr part3 getting_started_in_c_sharp
 
Antlr part1 introduction
Antlr part1 introductionAntlr part1 introduction
Antlr part1 introduction
 
Antlr part2 getting_started_in_java
Antlr part2 getting_started_in_javaAntlr part2 getting_started_in_java
Antlr part2 getting_started_in_java
 
3-use-casemodelling
3-use-casemodelling3-use-casemodelling
3-use-casemodelling
 
5-modular-design
5-modular-design5-modular-design
5-modular-design
 
4-architectural-views
4-architectural-views4-architectural-views
4-architectural-views
 
2-requirements-modelling
2-requirements-modelling2-requirements-modelling
2-requirements-modelling
 
1-requirements-elicitation
1-requirements-elicitation1-requirements-elicitation
1-requirements-elicitation
 
Analysis of Social Phenomena Using Machine Learning Techniques: A Mixed Resea...
Analysis of Social Phenomena Using Machine Learning Techniques: A Mixed Resea...Analysis of Social Phenomena Using Machine Learning Techniques: A Mixed Resea...
Analysis of Social Phenomena Using Machine Learning Techniques: A Mixed Resea...
 
Internet of Things: Middle-ware Platforms, Security, and Intrusion Detection
Internet of Things: Middle-ware Platforms, Security, and Intrusion DetectionInternet of Things: Middle-ware Platforms, Security, and Intrusion Detection
Internet of Things: Middle-ware Platforms, Security, and Intrusion Detection
 
Community Detection with Genetic Algorithm
Community Detection with Genetic AlgorithmCommunity Detection with Genetic Algorithm
Community Detection with Genetic Algorithm
 
SpotifyX Architectural Review
SpotifyX Architectural ReviewSpotifyX Architectural Review
SpotifyX Architectural Review
 
An overview of anomaly detection techniques
An overview of anomaly detection techniquesAn overview of anomaly detection techniques
An overview of anomaly detection techniques
 
SQLite and object-relational mapping in Java
SQLite and object-relational mapping in JavaSQLite and object-relational mapping in Java
SQLite and object-relational mapping in Java
 
Apache Mesos: Architecture, Design and Code Review
Apache Mesos: Architecture, Design and Code ReviewApache Mesos: Architecture, Design and Code Review
Apache Mesos: Architecture, Design and Code Review
 
یادگیری توالی به توالی با شبکه های عصبی
یادگیری توالی به توالی با شبکه های عصبییادگیری توالی به توالی با شبکه های عصبی
یادگیری توالی به توالی با شبکه های عصبی
 
Sequence to sequence learning with neural networks
Sequence to sequence learning with neural networksSequence to sequence learning with neural networks
Sequence to sequence learning with neural networks
 
Bridge Management System Using NoSQL Solutions
Bridge Management System Using NoSQL SolutionsBridge Management System Using NoSQL Solutions
Bridge Management System Using NoSQL Solutions
 
Extracting architectural model of software from source code
Extracting architectural model of software from source codeExtracting architectural model of software from source code
Extracting architectural model of software from source code
 
Software Fault Avoidance in Implementation
Software Fault Avoidance in ImplementationSoftware Fault Avoidance in Implementation
Software Fault Avoidance in Implementation
 

Recently uploaded

Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
Clustering techniques data mining book ....
Clustering techniques data mining book ....Clustering techniques data mining book ....
Clustering techniques data mining book ....ShaimaaMohamedGalal
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendArshad QA
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 

Recently uploaded (20)

Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
Clustering techniques data mining book ....
Clustering techniques data mining book ....Clustering techniques data mining book ....
Clustering techniques data mining book ....
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and Backend
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 

6-TDD

  • 1. 14/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir)
  • 2. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 2
  • 3. 34/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir)
  • 4. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 4 • Test driven development (TDD) is a software development approach. • In TDD a test is written before writing the code. • Once the new code passes the test, it is refactored to an acceptable standard.  TDD ensures that the source code is thoroughly unit tested and leads to modularized, flexible and extensible code.  TDD focuses on writing only the code necessary to pass tests, making the design simple and clear.
  • 5. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 5 • The test is written before testing the functionality • Ensures that the application is suitable for testability. • The functionality is implemented. • This is referred to as "red green refactor. • Red means fail and green shows a pass. • These steps are repeated. o The first goal of a programmer is to focus on the task at hand and to pass it.
  • 6. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 6
  • 7. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 7  TDD can lead to more modularized, flexible, and extensible code  Clean code  Leads to better design  Better code documentation  More productive  Good design
  • 8. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 8  My goal is for you to see the rhythm of test-driven development:  1. Quickly add a test  2. Run all tests and see the new one fail  3. Make a little change  4. Run all tests and see them all succeed  5. Refactor to remove duplication  The surprises are likely to be:  How each test can cover a small increment of functionality  How small and ugly the changes can be to make the new tests run  How often the tests are run  How many teensy tiny steps make up the refactorings
  • 9. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 9 1. Create class-diagram/Components-model using a class designer 2. Generate code from the diagram 3. Select a method to test 4. Generate test using Nunit, Junit, … 5. Run the test 6. The test fails 7. Complete the selected method 8. The test passes 9. Clean the code 10. Refactor the code 11. repeat Unit testing requires that the source code is composed in such a way that dependencies between modules can be easily neutralized with mocks. In addition, unit testing requires that functions are well isolated from each other
  • 10. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 10  Consider the class diagram, shown in the next slide.  The diagram is depicted in the Enterprise Architect environment.  Generate C# code from the class diagram.  Make a C# console application(.sln file).  Add the generated classes to the console application.  Use Nunit to generate test classes and unit test.
  • 11. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 11
  • 12. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 12
  • 13. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 13
  • 14. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 14 /////////////////////////////////////////////////////////// // point.cs // Implementation of the Class point // Generated by Enterprise Architect // Created on: 09-Apr-2020 5:44:14 PM // Original author: Asus /////////////////////////////////////////////////////////// using System; using System.Collections.Generic; using System.Text; using System.IO; using Geometry; namespace Geometry { public class point : GraphicalObject {
  • 15. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 15 public double x; public double y; public point(){ } ~point(){ } /// /// <param name="xCoord"></param> /// <param name="yCoord"></param> public point(double xCoord, double yCoord){ }
  • 16. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 16 /// <param name="p"></param> /// <param name="q"></param> public static double dist(point p, point q){ return 0; } /// /// <param name="p"></param> /// <param name="q"></param> public static double distPower2(point p, point q){ return 0; } public void draw(){ }
  • 17. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 17 public void dump(){ } /// /// <param name="p"></param> /// <param name="q"></param> /// <param name="r"></param> public static bool isCoLinear(point p, point q, point r){ return false; } /// /// <param name="aPoint"></param> public Boolean isEqual(point aPoint){ return null; }
  • 18. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 18 /// /// <param name="xc"></param> /// <param name="yc"></param> /// <param name="angel"></param> public void rotate(int xc, int yc, double angel){ } /// /// <param name="x_val"></param> /// <param name="y_val"></param> public void Set(double x_val, double y_val){ } }//end point }//end namespace Geometry
  • 19. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 19
  • 20. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 20 // This groups geometric objects as a whole object. class AccessModifiers { static void Main(string[] args) { point p1, p2, p3, p4; p1 = new point(1, 1); p2 = new point(3, 1); p3 = new point(1, 4); Triangels MyTriangel = new Triangels(p1, p2, p3); MyTriangel.rotate(1, 2, 30); //MyTriangel.draw(); p1.Set(1, 1); p2.Set(3, 1); p3.Set(3, 4); p4 = new point(1, 4); Quadrilateral MyQuadrilateral = new Quadrilateral(p1, p2, p3, p4); //MyQuadrilateral.draw(); GroupObject MyGroupObject = new GroupObject(); MyGroupObject.addObject(MyTriangel); MyGroupObject.addObject(MyQuadrilateral); MyGroupObject.draw(); Console.ReadLine(); }}}
  • 21. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 21
  • 22. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 22
  • 23. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 23  Use NUnit3 in visual studio environment to create test classes.  If Nunit is not installed you will have to install it.  Right click on the name of the method to be tested.  Select “Create unit test option”  A unit is the smallest possible part of a program that can be logically isolated and tested.  A method, function, procedure, or subroutine is an instance of a unit. Unit testing is the first phase of testing.  As an example, consider the class point, in C#, listed below:
  • 24. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 24 public class point : GraphicalObject { private double x, y; public point(double xCoord, double yCoord){} public void Set(double x_val, double y_val) {} // Computes the distance between the points P and q. public static double dist(point p, point q) {} public static double distPower2(point p, point q){} // Determines whether public static bool isCoLinear(point p, point q, point r){} public void dump(){} public Boolean isEqual(point aPoint){} // Rotates the point object around the point (xc, yc) for angle. public void rotate(int xc, int yc, double angel){} public void draw(){} }
  • 25. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 25  A unit is the smallest possible part of a program that can be logically isolated and tested.  A method, function, procedure, or subroutine is an instance of a unit. Unit testing is the first phase of testing.  As an example, right click on the name of the method “dist” in the class “point”, public static double dist(point p, point q) { … }  Select the “create test” option as shown in the bext slide.  If “Nunit” is not installed click on the “Get additional extensions”.
  • 26. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 26
  • 27. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 27
  • 28. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 28
  • 29. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 29 1. Test Cases are copied into x1, y1, x2 and y2, respectively. 2. Result is compared with the retuen value.
  • 30. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 30 The test cases show two points and their expected distances. Eg. [TestCase(0,0, 1,0, Result = 1)] P1 = (0,0) , P2 = (1,0) dist(P1, P2) = 1  To run tests select: Test->Run All Tests  To view test results: Test->Test Explorer
  • 31. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 31  The previous test fails.  Complete the body of the “dist” method as follows: // Computes the distance between the points p and q public static double dist(point p, point q) { double res = 0; if (p.x == q.x) { res = Math.Abs(p.y - q.y); return res; } if (p.y == q.y) { res = Math.Abs(p.x - q.x); return res; } double dx = p.x - q.x; double dy = p.y - q.y; res = Math.Sqrt(dx * dx + dy * dy); return res; }
  • 32. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 32  Run the tests again: Test->Run All Tests  To view test results: Test->Test Explorer
  • 33. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 33  Complete the test method.  The test fails: [Test()] [TestCase(0, 0, 90, 2, 0, Result = new double[2] { 0, 2 })] [TestCase(0, 0, 90, 1, 1, Result = new double[2] { 0, 1 })] [TestCase(0, 0, 90, 1, 0, Result = new double[2] { 0, 1 })] public double[] rotateTest(int xc, int yc, double angel, double x, double y) { point p = new point(x, y); p.rotate(xc, yc, angel); var result = new double[2]; result[0] = p.x; result[1] = p.y; Console.Write(" (x = {0}), y={1}", p.x, p.y); return result; }
  • 34. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 34  The previous test fails.  Complete the body of the “rotate” method as follows: // Rotates “this” object around point (xc, yc) for angle anel1 degree public void rotate(int xc, int yc, double angel) { //point ComparedWith = new point(xc, yc); angel = angel*3.141592/180; if (xc == x && yc == y) return; else { double cosa = Math.Cos(angel); double sina = Math.Sin(angel); double dx = x - xc; double dy = y - yc; x = (int)Math.Round(cosa * dx - sina * dy + xc); y = (int)Math.Round(sina * dx + cosa * dy + yc); } }
  • 35. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 35
  • 36. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 36 Write all the test cases for testing the “dist” method in a text file, testCases.txt. 0,0,0,1,1 0,0,2,0,2 0,0,3,4,5 0,0,0,0,0
  • 37. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 37 namespace Geometry.Tests { [TestFixture()] public class pointTests {[Test, TestCaseSource(typeof(MyTestCases), "TestCasesFromFile")] //[Test, TestCaseSource(typeof(MyTestCases), "TestCases")] public double distTest(point p, point q) { var result = point.dist(p, q); p.dump(); q.dump(); Console.WriteLine(" distance = {0}", result); return result; } }
  • 38. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 38 public class MyTestCases {public static IEnumerable TestCasesFromFile { get { using (StreamReader file = new StreamReader(@"D:TestCases.txt")) { string ln; while ((ln = file.ReadLine()) != null) { string[] values = ln.Split(','); point p = new point(Convert.ToDouble(values[0]), Convert.ToDouble(values[1])); point q = new point(Convert.ToDouble(values[2]), Convert.ToDouble(values[3])); yield return new TestCaseData(p, q).Returns(Convert.ToDouble(values[4])); } } } } } }
  • 39. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 39
  • 40. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 40  Requirements:  toRoman:  should return the Roman numeral representation for all integers 1 to 3999.  should always return a Roman numeral using uppercase letters.  fromRoman:  should take a valid Roman numeral and return the number that it represents.  should only accept uppercase Roman numerals (i.e. it should fail when given lowercase input).
  • 41. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 41 #roman.py """Convert to and from Roman numerals""" #Define exceptions class RomanError(Exception): pass class OutOfRangeError(RomanError): pass class NotIntegerError(RomanError): pass class InvalidRomanNumeralError(RomanError): pass def toRoman(n): """convert integer to Roman numeral""" pass def fromRoman(s): """convert Roman numeral to integer""" pass
  • 42. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 42 Unittest: testRoman.py """Unit test for roman.py""" import roman import unittes knownValues = ( (1, 'I'),.,.,.,..,(3999, 'MMMCMXCIX')) class TestToRoman(unittest.TestCase): def testToRomanGood(self): """toRoman should give known result with known input""" for integer, numeral in knownValues: result = roman.toRoman(integer) self.assertEqual(numeral, result) def testNonInteger(self): """toRoman should fail with non-integer input""" self.assertRaises(roman.NotIntegerError, roman.toRoman, 0.5)
  • 43. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 43 class TestFromRoman(unittest.TestCase): def setup(self): pass def tesrdown(self): pass def test_knownValues(self): """fromRoman should give known result with known input""" for integer, numeralin knownValues: result = roman.fromRoman(numeral) self.assertEqual(integer, result) def testTooManyRepeatedNumerals(self): """fromRoman should fail with too many repeated numerals""" for s in ('MMMM', 'DD', 'CCCC', 'LL', 'XXXX', 'VV', 'IIII'): self.assertRaises(roman.InvalidRomanNumeralError, roman.fromRoman, s)
  • 44. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 44 """Convert to and from Roman numerals""" #Define exceptions class RomanError(Exception): pass class OutOfRangeError(RomanError): pass class NotIntegerError(RomanError): pass class InvalidRomanNumeralError(RomanError): pass #Define digit mapping romanNumeralMap = (('M', 1000), ('CM', 900), ('D', 500),('CD', 400), ('C', 100), ('XC', 90), ('L', 50),('XL', 40), ('X', 10), ('IX', 9), ('V', 5), ('IV', 4), ('I', 1))
  • 45. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 45 def toRoman(n): """convert integer to Roman numeral""" if not (0 < n < 4000): raise OutOfRangeError, "number out of range (must be 1..3999)" if int(n) <> n: raise NotIntegerError, "non-integers can not be converted" result = "" for numeral, integer in romanNumeralMap: while n >= integer: result += numeral n -= integer return result def fromRoman(s): """convert Roman numeral to integer""" pass
  • 46. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 46 def fromRoman(s): """convert Roman numeral to integer""" result = 0 index = 0 for numeral, integer in romanNumeralMap: while s[index:index+len(numeral)] == numeral: result += integer index += len(numeral) return result
  • 47. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 47  The nose.tools module provides a number of testing aids: Decorators: for restricting test execution time and testing for exceptions, Assert: methods found in unittest.TestCase (only spelled in pep08 fashion, so assert_equal rather than assertEqual). nose.tools.raises(*exceptions) Test must raise one of expected exceptions to pass.  Example use: @raises(TypeError, ValueError) def test_raises_type_error(): raise TypeError("This test passes") nose.tools.assert_equal -- Fail if the two objects are unequal -- as determined by the ‘==’ operator. nose.tools.assert_false -- Fail the test if the expression is true.
  • 48. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 48 The overall architecture of the payroll application software consists of three packages.
  • 49. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 49 The “Guard interface” and “Time card reader” are two parallel tasks.
  • 50. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 50 There is a time card reader in the payroll department, as well.
  • 51. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 51 To assign a class to a component (In Rational-Rose environment) by: 1. Click on the component. 2. A list of all the classes will pop up. 3. Click on the name of a class, 4. Assign the class to the component.
  • 52. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 52
  • 53. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 53
  • 54. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 54 • [1] Erdogmus, Hakan; Morisio, Torchiano. On the Effectiveness of Test-first Approach to Programming. Proceedings of the IEEE Transactions on Software Engineering, 31(1). January 2005. (NRC 47445). Retrieved on 2008-01-14. • [2] George, Boby; Williams, Laurie. A Structured experiment of test-driven development. 2003. • [3] E.M. Maximilien, L. Williams; Assessing test-development at IBM, presented at International Conference of Software Engineering, Portland, OR, 2003. • [4] Beck, K. Test-Driven Development by Example, Addison Wesley, 2003 http://en.wikipedia.org/wiki/Test_driven_development#Test-Driven_Development_Cycle • [5] JMock.org, www.jmock.org, Year ?
  • 55. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 55 • What? • Software testing is the art of measuring and maintaining software quality. • Why? • To ensure that user expectations and requirements, business value, non- functional requirements, such as security, reliability and recoverability, and operational policies are all met. • How? • Testing strategies: Black, white, and gray box testing.*
  • 56. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 56 • How? • Testing strategies are traditionally divided into black, white, and gray box testing. - Black: The inside of the box (“solution implementation”) is dark. Testers focus only on input and output, typically when performing system and user acceptance testing. - White: The inside of the box is visible and analyzed as part of the testing. - Gray: A combination of black and white box testing typically used to test edge cases, which require an understanding of the internals and expected behavior.
  • 57. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 57
  • 58. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 58
  • 59. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 59
  • 60. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 60 • Microsoft Fakes is a new code isolation framework that can help you isolate code for testing by replacing other parts of the application with stubs or shims. • It allows you to test parts of your solution even if other parts of your app haven’t been implemented or aren’t working yet. • Microsoft Fakes come in two flavors:  Stubs … replace a class with a small substitute (“stub”) that implements the same interface.  Shims … modify the compiled code at run time, to inject and run a substitute (“shim”). • Stubs are typically used for calls within your solution that you can decouple using interfaces; shims are used for calls to referenced assemblies for which the code is not under your control.
  • 61. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 61 • Fakes come in two flavours: • A shim modifies the compiled code of your application at run time so that instead of making a specified method call, it runs the shim code that your test provides. Shims can be used to replace calls to assemblies that you cannot modify, such .NET assemblies. • A stub replaces a class with a small substitute that implements the same interface. To use stubs, you have to design your application so that each component depends only on interfaces, and not on other components.
  • 62. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 62 We have a class that implements something depends on System library. using System; namespace DemoClassLibrary { public class Foo { public long UtcNowTick() { return DateTime.UtcNow.Ticks; } } } So this UtcNowTick is depends on the DateTime, and right now we want to test this function This is a typical scenario for us to use shim to test because we cannot control the behaviour of the system library.
  • 63. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 63 • Add the fake to the desired library As a result a fake assembly will be created: after-add-fake.jpg
  • 64. 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir) 64  So right now, we have the fake assembly for system now, and we can control the behaviour of system namespace api as below:
  • 65. Resources – TDD & refactoring test-driven development: A Practical Guide Dave Astels Prentice-Hall/Pearson Education, 2003 ISBN 0-13-101649-0 ___________________________ Test-Driven Development: By Example Kent Beck Addison-Wesley, 2003 ISBN 0-321-14653-0 654/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir)
  • 66. Resources – TDD & refactoring Agile Java - Crafting Code with Test-Driven Development Jeff Langr Prentice Hall 2005 ISBN 0-13-148239-4 66 4/20/2020 S. Parsa, Associate Professor (www.parsa.iust.ac.ir)