Skip to content

Instantly share code, notes, and snippets.

@larainema
Created June 19, 2015 04:49
Show Gist options
  • Save larainema/de43f4768a00bf02bd9d to your computer and use it in GitHub Desktop.
Save larainema/de43f4768a00bf02bd9d to your computer and use it in GitHub Desktop.
Test-driven development
Class Stack():
def __init__(self,size):
self.size=size
self.stack=[]
self.top=-1
def push(self,n):
if self.isfull():
print "Out of range"
else:
self.stack.append(n)
self.top=self.top+1
def pop(self):
if self.isempty():
print "Stack is empty"
else:
self.top=self.top-1
return self.stack.pop()
def isfull(self):
return self.top+1==self.size
def isempty(self):
return self.top==-1
Test-driven development (TDD) is a software development process that relies on the repetition of a very short development cycle: first the developer writes an (initially failing) automated test case that defines a desired improvement or new function, then produces the minimum amount of code to pass that test, and finally refactors the new code to acceptable standards. Kent Beck, who is credited with having developed or 'rediscovered'[1] the technique, stated in 2003 that TDD encourages simple designs and inspires confidence.
A commonly applied structure for test cases has (1) setup, (2) execution, (3) validation, and (4) cleanup.
Stack test case:
1. s = Stack(10) print s.size
2. s.push(1)
3. s.isfull()
4. s.pop()
5. s.isempty()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment