Skip to content

Instantly share code, notes, and snippets.

@nipunbatra
Created May 10, 2013 07:17
Show Gist options
  • Save nipunbatra/5552901 to your computer and use it in GitHub Desktop.
Save nipunbatra/5552901 to your computer and use it in GitHub Desktop.
'''Importing the functions to be unit tested'''
from smart_meter import create_header_row
from nose import with_setup
'''Creating a setup method to initialize variables which can be used more than once during multiple tests'''
def setup_lists():
global parameter_list, parameter_numbers
parameter_list=["timestamp","W","W1","A1"]
parameter_numbers=[1,2]
@with_setup(setup_lists)
def test_create_header_row():
'''These things should be tested within the function. Since they are not the function would be failing these test cases.
Thus from unit testing we can learn that we need to add these aserts, exceptional handling in the function
'''
#Assert that length of parameter_numbers is <= length of parameter_list
assert len(parameter_list)>=len(parameter_numbers), "Length of parameter numbers should be lesser than equal to parameter list"
#Assert that parameter numbers vary from 0 to len(parameter_list)-1
#This is imporatant since we may forget 0 indexing and start from 1
assert min(parameter_numbers)>=0 and max(parameter_numbers)<=len(parameter_numbers)
'''Now actual function call is made'''
header=create_header_row(parameter_list, parameter_numbers)
#Assert that output obtained from the function is a string
assert type(header)==type("a")
#Assert that the number of commas is equal to the length of parameter_list
assert header.count(",")==len(parameter_numbers)
#Assert that the first parameter in header is always timestamp
assert header.split(",")[0]=="timestamp"
#Assert that the header ends with a new line
assert header[-1]=="\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment