Python Unit test

toluagboola at gmail.com toluagboola at gmail.com
Tue Jan 26 23:57:19 EST 2016


On Tuesday, January 26, 2016 at 10:57:51 PM UTC+2, 4ndre4 wrote:
> On 26/01/2016 11:09, toluagboola at gmail.com wrote:
> 
> [...]
> > I'm a University student of IT with majors in Embedded Systems.
>  > I have this assignment where I have to write Unittest for a simple 
> Python Code
>  > without having prior knowledge of Python. I really need some help.
> 
> www.python.org to get a basic grasp on Python. There is a very good 
> tutorial there.
> 
> As for unit testing, you need to know a bit of the the theory behind it, 
> and how to use it in Python.
> 
> The following are four very good books about unit testing, in general:
> 
> Effective Unit Testing: A guide for Java developers - Lasse Koskela
> Test Driven: TDD and Acceptance TDD for Java Developers - Lasse Koskela
> Test-Driven Development: By Example - Kent Beck
> 
> This is a good book but with examples in C# (you might just get the 
> logic behind them):
> The Art of Unit Testing: with examples in C# - Roy Osherove
> 
> I am pretty sure that on Amazon you can find many others about specific 
> unit testing in Python.
> 
> On YouTube, there's a good number of videos about unit testing.
> This one, for example: https://www.youtube.com/watch?v=TWBDa5dqrl8
> 
> -- 
> 4ndre4
> "The use of COBOL cripples the mind; its teaching should, therefore, be 
> regarded as a criminal offense." (E. Dijkstra)

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



More information about the Python-list mailing list