reducing boilerplate and cleaning up after writing few small testswe often find that we have to do the same setup code for several related tests for examplethe following list subclass has three methods for statistical calculationsfrom collections import defaultdict class statslist(list)def mean(self)return sum(selflen(selfdef median(self)if len(self return self[int(len(self )elseidx int(len(self return (self[idxself[idx- ] def mode(self)freqs defaultdict(intfor item in selffreqs[item+ mode_freq max(freqs values()modes [for itemvalue in freqs items()if value =mode_freqmodes append(itemreturn modes clearlywe're going to want to test situations with each of these three methods that have very similar inputswe'll want to see what happens with empty lists or with lists containing non-numeric values or with lists containing normal dataset we can use the setup method on the testcase class to do initialization for each test this method accepts no argumentsand allows us to do arbitrary setup before each test is run for examplewe can test all three methods on identical lists of integers as followsfrom stats import statslist import unittest class testvalidinputs(unittest testcase)def setup(self)self stats statslist([ , , , , , ]def test_mean(self)