a test for if an instance was created

Ok,  I tried to put comments in to explain it. 

********* first the file that holds the class's and methods ***********

example.py


class Example(object):
# this is just for testing. I want to show how you can test it.
def print_something(self):
# def a string to return for test...
some_string = "I am going to make this work"
#return that string, could be anything
return some_string
class MakeSomething(object):
# we are going to give the instance of MakeSomething two
# attributes when it's initiated. A name and a dictionary
# which will hold the call for our Example class when we
# create it.
def __init__(self, name):
# the has has an attribute, name which you pass
# in with the paramaters makesomething = MakeSomething('name')
# it also has a dictionary named routes, that belongs to only
# the class MakeSomething, and the instance you create
self.name = name
self.routes = {}
# this will return a value for direction, which in this
# case we're going to make an instance of class Example further down
def go(self, direction):
return self.routes.get(direction, None)
# we are going to add a 'route' to the routes that can
# take you to a different instance, in this case an instance
# of Example class
def add_route(self, routes):
self.routes.update(routes)
# lets make an instance of MakeSomething....
# give it a name
makesomething = MakeSomething('a_place')
# lets add a route that will take us to the Example,
# when we type 'Example()' that creates an instance and runs
# the code inside.
makesomething.add_route({
'go to example': Example()
})

Now the file that holds the test. This test works on nosetest or pytest.

*********example_test.py**********
from nose.tools import*
from example import*

# we import the stuff we are testing to the file we test

def test_example():
# What this is doing is saying that our 'makesomething'
# is an instance of the MakeSomething class/object type
assert_true(isinstance(makesomething, MakeSomething))
# now we're going to see if makesomething will make an
# instance of example like we wanted.
gosomewhere = makesomething.go('go to example')
# lest see if that triggered the 'Example()' instance
assert_true(isinstance(gosomewhere, Example))
# and we can test that the Example.print_something returns a string
assert_true(isinstance(gosomewhere.print_something(), str))

Comments

Popular posts from this blog

playing with color in powershell python

JavaScript Ascii animation with while loops and console.log

playing with trigonometry sin in pygame