Python in a Nutshell (Testing Tips)

Testing

Unit Testing and System Testing

Unit testing means writing and running tests to exercise a single module or an even smaller unit, such as a class or function.

System testing (also known as functional or integration testing) involves running an entire program with known inputs.

Some classic books on testing also draw the distinction between white-box testing, done with knowledge of a program’s internals, and black-box testing, done without such knowledge.

The doctest Module

is_palindrome.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
def is_palindrome(s):
	''' (str) -> bool
	Return Ture if and only if is a palindrome
	>>> is_palindrome('noon')
	True
	>>> is_palindrome('people')
	False
	>>> is_palindrome('catac')
	True
	'''
	
	i = 0
	j = len(s) - 1
	while (i<j) and (s[i] == s[j]):
		i = i + 1
		j = j - 1
	return j <= i

if __name__ == '__main__': 
    import doctest
    doctest.testmod( )

In each docstring, testmod finds all examples (by looking for occurrences of the interpreter prompt '>>> ', possibly preceded by whitespace) and runs each example.

The unittest Module

The TestCase class

1
2
3
4
5
6
7
8
9
10
t.assert_(condition,msg=None)
t.assertAlmostEqual(first,second,places=7,msg=None)
t.assertEqual(first,second,msg=None)
t.assertNotAlmostEqual(first,second,places=7,msg=None)
t.assertNotEqual(first,second,msg=None)
t.assertRaises(exceptionSpec,callable,*args,**kwargs)
t.fail(msg=None)
t.failIf(condition, msg=None)
t.setUp( )
t.tearDown( )

完整例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import unittest
import is_palindrome

class TestIsPalindrome(unittest.TestCase):
    def test1(self):
        actual     = is_palindrome.is_palindrome('noon')
        expected   = Ture
        self.assertEqual(actual,expected)
    
    def test2(self):
        actual     = is_palindrome.is_palindrome('people')
        expected   = False
        self.assertEqual(actual,expected)
    
    def test3(self):
        actual     = is_palindrome.is_palindrome('actca')
        expected   = Ture
        self.assertEqual(actual,expected)
    
    def test4(self):
        self.assertEqual(is_palindrome.is_palindrome('actca'),True)
     
if __name__ == '__main__': 
     unittest.main(exit=False)

Choosing Testing Cases

  • Size: For collections, 0 , 1 and several items
  • Dichotomies: Vowels/Non-vowels, even/odd, positive/negative, empty/full
  • Boundaries: test all that threshold
  • Order: different orders

Debugging

The inspect Module

The traceback Module

The pdb Module

Optimization

Benchmarking

Let L be any list,T any string (plain or Unicode); D any dict; S any set, with (say) numbers as items (with O(1) hashing and comparison) and x any number:

O(1): len(L), len(T), len(D), len(S), L[i], T[i], D[i], del D[i], if x in D, if x in S, S.add(x), S.remove(x), additions or removals to/from the right end of L

O(N): Loops on L, T, D, S, general additions or removals to/from L (not at the right end), all methods on T, if x in L, if x in T, most methods on L, all shallow copies

O(N log N): L.sort in general (but O(N) if L is already nearly sorted or reverse-sorted

use poc_simpletest

1
2
3
4
5
6
7
8
9
#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import poc_format_testsuite

def myFunc(paras):
    pass

poc_format_testsuite.run_test(myFunc(paras))
1
2
3
4
5
6
7
8
9
10
11
imoport poc_simpletest

def run_test():
    suite = poc_simpletest.TestSuite()
    suite.run_test(formate_function(0),"0:00.0" "Test #1")
    suite.run_test(formate_function(7),"0:00.7" "Test #2")
    suite.run_test(formate_function(17),"0:01.7" "Test #3")
    suite.run_test(formate_function(60),"0:06.0" "Test #4")
    suite.run_test(formate_function(63),"0:06.3" "Test #5")
    suite.run_test(formate_function(5999),"9:59.9" "Test #6")
    suite.report_results()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
wget https://d396qusza40orc.cloudfront.net/datasci/lecture_slides/week4/026_nosql_intro.pdf
wget https://d396qusza40orc.cloudfront.net/datasci/lecture_slides/week4/027_eventual_consistency.pdf
wget https://d396qusza40orc.cloudfront.net/datasci/lecture_slides/week4/028_memcached.pdf
wget https://d396qusza40orc.cloudfront.net/datasci/lecture_slides/week4/029_dynamo.pdf
wget https://d396qusza40orc.cloudfront.net/datasci/lecture_slides/week4/030_couchdb.pdf
wget https://d396qusza40orc.cloudfront.net/datasci/lecture_slides/week4/031_bigtable.pdf
wget https://d396qusza40orc.cloudfront.net/datasci/lecture_slides/week4/032_other_google_systems.pdf
wget https://d396qusza40orc.cloudfront.net/datasci/lecture_slides/week4/033_nosql_response.pdf
wget https://d396qusza40orc.cloudfront.net/datasci/lecture_slides/week4/034_pig_intro.pdf
wget https://d396qusza40orc.cloudfront.net/datasci/lecture_slides/week4/035_pig_load_filter_group_foreach.pdf
wget https://d396qusza40orc.cloudfront.net/datasci/lecture_slides/week4/036_cogroup_join.pdf
wget https://d396qusza40orc.cloudfront.net/datasci/lecture_slides/week4/037_pig_evaluation.pdf

wget https://d396qusza40orc.cloudfront.net/datasci/lecture_slides/week5/038_stats_intro.pdf https://d396qusza40orc.cloudfront.net/datasci/lecture_slides/week5/039_publication_bias.pdf https://d396qusza40orc.cloudfront.net/datasci/lecture_slides/week5/040_effect_size_meta_analysis_heteroskedasticity.pdf https://d396qusza40orc.cloudfront.net/datasci/lecture_slides/week5/041_fraud_benfords_law.pdf https://d396qusza40orc.cloudfront.net/datasci/lecture_slides/week5/042_multiple_hypothesis_testing_CORRECTED.pdf https://d396qusza40orc.cloudfront.net/datasci/lecture_slides/week5/043_recap_and_big_data.pdf https://d396qusza40orc.cloudfront.net/datasci/lecture_slides/week5/044_intro_bayesian.pdf https://d396qusza40orc.cloudfront.net/datasci/lecture_slides/week5/045_bayes_rule.pdf

wget https://d396qusza40orc.cloudfront.net/datasci/lecture_slides/week6/047_overview_machine_learning.pdf https://d396qusza40orc.cloudfront.net/datasci/lecture_slides/week6/048_intro_machine_learning_2.pdf https://d396qusza40orc.cloudfront.net/datasci/lecture_slides/week6/049_rules_1.pdf https://d396qusza40orc.cloudfront.net/datasci/lecture_slides/week6/050_rules_2.pdf https://d396qusza40orc.cloudfront.net/datasci/lecture_slides/week6/051_intro_trees.pdf https://d396qusza40orc.cloudfront.net/datasci/lecture_slides/week6/052_information_gain.pdf https://d396qusza40orc.cloudfront.net/datasci/lecture_slides/week6/053_overfitting.pdf https://d396qusza40orc.cloudfront.net/datasci/lecture_slides/week6/054_evaluation_thresholds.pdf https://d396qusza40orc.cloudfront.net/datasci/lecture_slides/week6/055_bootstrap.pdf https://d396qusza40orc.cloudfront.net/datasci/lecture_slides/week6/056_ensembles_and_boosting.pdf https://d396qusza40orc.cloudfront.net/datasci/lecture_slides/week6/057_random_forests.pdf https://d396qusza40orc.cloudfront.net/datasci/lecture_slides/week6/058_nearest_neighbor.pdf

wget https://d396qusza40orc.cloudfront.net/datasci/lecture_slides/week6/060_gradient_descent_part_2.pdf https://d396qusza40orc.cloudfront.net/datasci/lecture_slides/week6/061_intuition_logistic_regression_svms.pdf https://d396qusza40orc.cloudfront.net/datasci/lecture_slides/week6/062_intuition_regularization.pdf https://d396qusza40orc.cloudfront.net/datasci/lecture_slides/week6/063_stochastic_gradient_descent.pdf https://d396qusza40orc.cloudfront.net/datasci/lecture_slides/week6/064_unsupervised_learning_copy.pdf https://d396qusza40orc.cloudfront.net/datasci/lecture_slides/week6/065_kmeans.pdf https://d396qusza40orc.cloudfront.net/datasci/lecture_slides/week6/066_dbscan.pdf

wget https://d396qusza40orc.cloudfront.net/datasci/lecture_slides/week8/083_prism_example.pdf
wget https://d396qusza40orc.cloudfront.net/datasci/lecture_slides/week8/084_evaluating_recursive_programs.pdf
wget https://d396qusza40orc.cloudfront.net/datasci/lecture_slides/week8/085_optimizing_recursive_programs_in_mr.pdf
wget https://d396qusza40orc.cloudfront.net/datasci/lecture_slides/week8/086_graph_representations.pdf

wget https://class.coursera.org/datasci-002/lecture/download.mp4?lecture_id=217
wget https://class.coursera.org/datasci-002/lecture/download.mp4?lecture_id=219