Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #102153
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Python Unit test |
| Date | 2016-01-27 07:54 -0500 |
| Organization | IISS Elusive Unicorn |
| Message-ID | <mailman.33.1453899255.2338.python-list@python.org> (permalink) |
| References | <e6d96722-9d71-4bc5-9f98-33eeaa09d70d@googlegroups.com> <n88meg$98e$8@dont-email.me> <eb7a3739-fe65-453f-9df7-552e2d90579d@googlegroups.com> |
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/
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Python Unit test toluagboola@gmail.com - 2016-01-26 03:09 -0800
Re: Python Unit test David Palao <dpalao.python@gmail.com> - 2016-01-26 12:55 +0100
Re: Python Unit test toluagboola@gmail.com - 2016-01-26 04:26 -0800
Re: Python Unit test David Palao <dpalao.python@gmail.com> - 2016-01-26 14:48 +0100
Re: Python Unit test Bernardo Sulzbach <mafagafogigante@gmail.com> - 2016-01-26 12:24 -0200
Re: Python Unit test Tolu Agboola <toluagboola@gmail.com> - 2016-01-26 17:48 +0200
Re: Python Unit test Joel Goldstick <joel.goldstick@gmail.com> - 2016-01-26 10:56 -0500
Re: Python Unit test David Palao <dpalao.python@gmail.com> - 2016-01-26 17:04 +0100
Re: Python Unit test 4ndre4 <4ndre4@4ndre4.com.invalid> - 2016-01-26 20:57 +0000
Re: Python Unit test toluagboola@gmail.com - 2016-01-26 20:57 -0800
Re: Python Unit test Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2016-01-27 07:54 -0500
Re: Python Unit test toluagboola@gmail.com - 2016-01-28 08:57 -0800
Re: Python Unit test 4ndre4 <4ndre4@4ndre4.com.invalid> - 2016-01-30 18:03 +0000
csiph-web