我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用unittest.html()。
def group(s, n): # See # http://www.python.org/doc/2.6/library/functions.html#zip return zip(*[iter(s)]*n)
def interleave_planes(ipixels, apixels, ipsize, apsize): """ Interleave (colour) planes, e.g. RGB + A = RGBA. Return an array of pixels consisting of the `ipsize` elements of data from each pixel in `ipixels` followed by the `apsize` elements of data from each pixel in `apixels`. Conventionally `ipixels` and `apixels` are byte arrays so the sizes are bytes, but it actually works with any arrays of the same type. The returned array is the same type as the input arrays which should be the same type as each other. """ itotal = len(ipixels) atotal = len(apixels) newtotal = itotal + atotal newpsize = ipsize + apsize # Set up the output buffer # See http://www.python.org/doc/2.4.4/lib/module-array.html#l2h-1356 out = array(ipixels.typecode) # It's annoying that there is no cheap way to set the array size :-( out.extend(ipixels) out.extend(apixels) # Interleave in the pixel data for i in range(ipsize): out[i:newtotal:newpsize] = ipixels[i:itotal:ipsize] for i in range(apsize): out[i+ipsize:newtotal:newpsize] = apixels[i:atotal:apsize] return out
def mycallersname(): """Returns the name of the caller of the caller of this function (hence the name of the caller of the function in which "mycallersname()" textually appears). Returns None if this cannot be determined.""" # http://docs.python.org/library/inspect.html#the-interpreter-stack import inspect frame = inspect.currentframe() if not frame: return None frame_,filename_,lineno_,funname,linelist_,listi_ = ( inspect.getouterframes(frame)[2]) return funname
def _enhex(s): """Convert from binary string (bytes) to hex string (str).""" import binascii return bytestostr(binascii.hexlify(s)) # Copies of PngSuite test files taken # from http://www.schaik.com/pngsuite/pngsuite_bas_png.html # on 2009-02-19 by drj and converted to hex. # Some of these are not actually in PngSuite (but maybe they should # be?), they use the same naming scheme, but start with a capital # letter.
def group(s, n): # See # http://www.python.org/doc/2.6/library/functions.html#zip return list(zip(*[iter(s)]*n))
def testAssertMultiLineEqual(self): sample_text = b"""\ http://www.python.org/doc/2.3/lib/module-unittest.html test case A test case is the smallest unit of testing. [...] """ revised_sample_text = b"""\ http://www.python.org/doc/2.4.1/lib/module-unittest.html test case A test case is the smallest unit of testing. [...] You may provide your own implementation that does not subclass from TestCase, of course. """ sample_text_error = b"""\ - http://www.python.org/doc/2.3/lib/module-unittest.html ? ^ + http://www.python.org/doc/2.4.1/lib/module-unittest.html ? ^^^ test case - A test case is the smallest unit of testing. [...] + A test case is the smallest unit of testing. [...] You may provide your ? +++++++++++++++++++++ + own implementation that does not subclass from TestCase, of course. """ self.maxDiff = None for type_changer in (lambda x: x, lambda x: x.decode('utf8')): try: self.assertMultiLineEqual(type_changer(sample_text), type_changer(revised_sample_text)) except self.failureException, e: # need to remove the first line of the error message error = str(e).encode('utf8').split('\n', 1)[1] # assertMultiLineEqual is hooked up as the default for # unicode strings - so we can't use it for this check self.assertTrue(sample_text_error == error)
def testAssertMultiLineEqual(self): sample_text = """\ http://www.python.org/doc/2.3/lib/module-unittest.html test case A test case is the smallest unit of testing. [...] """ revised_sample_text = """\ http://www.python.org/doc/2.4.1/lib/module-unittest.html test case A test case is the smallest unit of testing. [...] You may provide your own implementation that does not subclass from TestCase, of course. """ sample_text_error = """\ - http://www.python.org/doc/2.3/lib/module-unittest.html ? ^ + http://www.python.org/doc/2.4.1/lib/module-unittest.html ? ^^^ test case - A test case is the smallest unit of testing. [...] + A test case is the smallest unit of testing. [...] You may provide your ? +++++++++++++++++++++ + own implementation that does not subclass from TestCase, of course. """ self.maxDiff = None try: self.assertMultiLineEqual(sample_text, revised_sample_text) except self.failureException as e: # need to remove the first line of the error message error = str(e).split('\n', 1)[1] # no fair testing ourself with ourself, and assertEqual is used for strings # so can't use assertEqual either. Just use assertTrue. self.assertTrue(sample_text_error == error)
def make_test_program_class(extra_tests): """ Return a subclass of unittest.TestProgram. """ # The function unittest.main() is an alias for unittest.TestProgram's # constructor. TestProgram's constructor does the following: # # 1. calls self.parseArgs(argv), # 2. which in turn calls self.createTests(). # 3. then the constructor calls self.runTests(). # # The createTests() method sets the self.test attribute by calling one # of self.testLoader's "loadTests" methods. Each loadTest method returns # a unittest.TestSuite instance. Thus, self.test is set to a TestSuite # instance prior to calling runTests(). class PystacheTestProgram(TestProgram): """ Instantiating an instance of this class runs all tests. """ def createTests(self): """ Load tests and set self.test to a unittest.TestSuite instance Compare-- http://docs.python.org/library/unittest.html#unittest.TestSuite """ super(PystacheTestProgram, self).createTests() self.test.addTests(extra_tests) return PystacheTestProgram # Do not include "test" in this function's name to avoid it getting # picked up by nosetests.
def round_trip_pickle(self, obj, path=None): if path is None: path = u('__%s__.pickle' % rands(10)) with ensure_clean(path) as path: pd.to_pickle(obj, path) return pd.read_pickle(path) # https://docs.python.org/3/library/unittest.html#deprecated-aliases
def write_pnm(file, width, height, pixels, meta): """Write a Netpbm PNM/PAM file.""" bitdepth = meta['bitdepth'] maxval = 2**bitdepth - 1 # Rudely, the number of image planes can be used to determine # whether we are L (PGM), LA (PAM), RGB (PPM), or RGBA (PAM). planes = meta['planes'] # Can be an assert as long as we assume that pixels and meta came # from a PNG file. assert planes in (1,2,3,4) if planes in (1,3): if 1 == planes: # PGM # Could generate PBM if maxval is 1, but we don't (for one # thing, we'd have to convert the data, not just blat it # out). fmt = 'P5' else: # PPM fmt = 'P6' file.write('%s %d %d %d\n' % (fmt, width, height, maxval)) if planes in (2,4): # PAM # See http://netpbm.sourceforge.net/doc/pam.html if 2 == planes: tupltype = 'GRAYSCALE_ALPHA' else: tupltype = 'RGB_ALPHA' file.write('P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\n' 'TUPLTYPE %s\nENDHDR\n' % (width, height, planes, maxval, tupltype)) # Values per row vpr = planes * width # struct format fmt = '>%d' % vpr if maxval > 0xff: fmt = fmt + 'H' else: fmt = fmt + 'B' for row in pixels: file.write(struct.pack(fmt, *row)) file.flush()
def load_tests(loader, tests, pattern): """ Add tests within doctests so that the unittest runner picks them up. See https://docs.python.org/3.5/library/unittest.html#load-tests-protocol """ etl_modules = sorted(mod for mod in sys.modules if mod.startswith("etl")) logger.info("Adding tests from %s", etl.names.join_with_quotes(etl_modules)) for mod in etl_modules: tests.addTests(doctest.DocTestSuite(mod)) return tests