Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Dennis Lee Bieber Newsgroups: comp.lang.python Subject: Re: Python Unit test Date: Wed, 27 Jan 2016 07:54:25 -0500 Organization: IISS Elusive Unicorn Lines: 69 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de /RVRaLilK1/eQGWNfcYzOA/kr8TgBwNOsmXMcrICyC8Q== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'args': 0.04; 'subject:Python': 0.05; 'sys': 0.05; '__name__': 0.07; 'main()': 0.07; 'subject:test': 0.07; 'unittest': 0.07; "%s'": 0.09; 'message-id:@4ax.com': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'sys.argv[0]': 0.09; 'through.': 0.09; 'python': 0.10; 'jan': 0.11; 'def': 0.13; "'%d": 0.16; "'__main__':": 0.16; '2016': 0.16; 'decode': 0.16; 'main():': 0.16; 'opts:': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'skip:} 10': 0.16; 'sys.exit(1)': 0.16; 'looked': 0.16; 'skip:` 10': 0.18; 'try:': 0.18; 'url:home': 0.18; "aren't": 0.22; 'dropped': 0.22; 'module': 0.25; 'header:X-Complaints-To:1': 0.26; 'compare': 0.27; "skip:' 10": 0.28; 'looks': 0.29; 'invoke': 0.29; 'relies': 0.29; 'print': 0.30; 'creating': 0.30; 'code': 0.30; 'operations': 0.31; 'maybe': 0.33; 'stream': 0.33; 'tue,': 0.34; 'except': 0.34; 'feed': 0.35; 'skip:> 10': 0.35; 'skip:p 30': 0.35; 'unit': 0.35; 'there': 0.36; 'possible': 0.36; 'to:addr :python-list': 0.36; 'subject:: ': 0.37; 'being': 0.37; 'received:org': 0.37; 'charset:us-ascii': 0.37; 'itself': 0.38; 'anything': 0.38; 'end': 0.39; 'means': 0.39; 'test': 0.39; 'rather': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'some': 0.40; 'email addr:gmail.com': 0.62; 'confirm': 0.62; 'more': 0.63; '20,': 0.66; 'results': 0.66; 'results.': 0.67; 'connection,': 0.72; '>def': 0.84; '>if': 0.84; 'dennis': 0.91 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: adsl-108-68-177-68.dsl.klmzmi.sbcglobal.net X-Newsreader: Forte Agent 6.00/32.1186 X-No-Archive: YES X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:102153 On Tue, 26 Jan 2016 20:57:19 -0800 (PST), toluagboola@gmail.com declaimed the following: >import getopt, sys >import dpkt, pcap > >def usage(): > print >>sys.stderr, 'usage: %s [-i device] [pattern]' % sys.argv[0] > sys.exit(1) > >def main(): > opts, args = getopt.getopt(sys.argv[1:], 'i:h') > name = None > for o, a in opts: > if o == '-i': name = a > else: usage() > > pc = pcap.pcap(name) > pc.setfilter(' '.join(args)) > decode = { pcap.DLT_LOOP:dpkt.loopback.Loopback, > pcap.DLT_NULL:dpkt.loopback.Loopback, > pcap.DLT_EN10MB:dpkt.ethernet.Ethernet }[pc.datalink()] > try: > print 'listening on %s: %s' % (pc.name, pc.filter) > for ts, pkt in pc: > print ts, `decode(pkt)` > except KeyboardInterrupt: > nrecv, ndrop, nifdrop = pc.stats() > print '\n%d packets received by filter' % nrecv > print '%d packets dropped by kernel' % ndrop > >if __name__ == '__main__': > main() > > >Here is what the python code looks like and I am to make a Unittest for it Off-hand -- you will find a "unit" test to be rather difficult for that example; there aren't any "units" that can be readily tested. The unittest module relies upon creating methods that invoke operations in the program with known inputs, and that then compare the known/expected results to what the operation has produced. At the least, from what I can tell, that means you have to mock up some way to feed a known stream of packets to the Ethernet connection, possible with a known timestamp, which satisfies the filter... and maybe some packets that don't so you can confirm they don't get through. You don't have anything "callable" that returns results. If the end of main() looked more like: except KeyboardInterrupt: #don't know how to do that in a test return pc.stats() THEN you might be able to create a unittest where the unit being tested is main itself def test_main(...): nr, nd, ni = main() #somehow it needs to end assertEqual(nr, 20, "received packet count not 20") which relies upon somehow having a known feed to the packet sniffer, and some way to end the main function. -- Wulfraed Dennis Lee Bieber AF6VN wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/