Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #102109 > unrolled thread

Python Unit test

Started bytoluagboola@gmail.com
First post2016-01-26 03:09 -0800
Last post2016-01-30 18:03 +0000
Articles 13 — 7 participants

Back to article view | Back to comp.lang.python


Contents

  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

#102109 — Python Unit test

Fromtoluagboola@gmail.com
Date2016-01-26 03:09 -0800
SubjectPython Unit test
Message-ID<e6d96722-9d71-4bc5-9f98-33eeaa09d70d@googlegroups.com>
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.
Thanks

[toc] | [next] | [standalone]


#102110

FromDavid Palao <dpalao.python@gmail.com>
Date2016-01-26 12:55 +0100
Message-ID<mailman.2.1453809338.2338.python-list@python.org>
In reply to#102109
Hello,
It would be good to provide more details on what you need help for.
For instance, have you read the manual
https://docs.python.org/3/library/unittest.html
?
It even contains a couple of examples.

Best

2016-01-26 12:09 GMT+01:00  <toluagboola@gmail.com>:
> 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.
> Thanks
> --
> https://mail.python.org/mailman/listinfo/python-list

[toc] | [prev] | [next] | [standalone]


#102111

Fromtoluagboola@gmail.com
Date2016-01-26 04:26 -0800
Message-ID<658abeb0-d14e-4e0a-a3fe-f128a198840c@googlegroups.com>
In reply to#102110
Yes I have studied the page but it's quite vague to me. So we have this project - Development of a touch screen controllable IEC61850 standard protocol analyzer. With this analyzer user can monitor and analyze network traffic in substation network.

[toc] | [prev] | [next] | [standalone]


#102113

FromDavid Palao <dpalao.python@gmail.com>
Date2016-01-26 14:48 +0100
Message-ID<mailman.4.1453816110.2338.python-list@python.org>
In reply to#102111
Well, there is a very specific example in there.
But you still don't give us much information about *concrete* troubles you have.
I *guess* that one of the following is true:
1) You have problems to understand some part of the example in the
unittest module documentation.
2) You don't know how to apply those ideas to some specific code you have.
3) something else.
Could you please be more specific?

Best

2016-01-26 13:26 GMT+01:00  <toluagboola@gmail.com>:
> Yes I have studied the page but it's quite vague to me. So we have this project - Development of a touch screen controllable IEC61850 standard protocol analyzer. With this analyzer user can monitor and analyze network traffic in substation network.
> --
> https://mail.python.org/mailman/listinfo/python-list

[toc] | [prev] | [next] | [standalone]


#102116

FromBernardo Sulzbach <mafagafogigante@gmail.com>
Date2016-01-26 12:24 -0200
Message-ID<mailman.7.1453818324.2338.python-list@python.org>
In reply to#102111
Or maybe he doesn't know enough about unit testing. If that is the
case, Wikipedia and SO should have you covered. Amazon has a few books
on the subject, too.

[toc] | [prev] | [next] | [standalone]


#102126

FromTolu Agboola <toluagboola@gmail.com>
Date2016-01-26 17:48 +0200
Message-ID<mailman.16.1453823472.2338.python-list@python.org>
In reply to#102111
I'll go with number 2. I don't know how to apply the ideas to the code I have. 

Sent from my iPhone

> On 26 Jan 2016, at 15:48, David Palao <dpalao.python@gmail.com> wrote:
> 
> Well, there is a very specific example in there.
> But you still don't give us much information about *concrete* troubles you have.
> I *guess* that one of the following is true:
> 1) You have problems to understand some part of the example in the
> unittest module documentation.
> 2) You don't know how to apply those ideas to some specific code you have.
> 3) something else.
> Could you please be more specific?
> 
> Best
> 
> 2016-01-26 13:26 GMT+01:00  <toluagboola@gmail.com>:
>> Yes I have studied the page but it's quite vague to me. So we have this project - Development of a touch screen controllable IEC61850 standard protocol analyzer. With this analyzer user can monitor and analyze network traffic in substation network.
>> --
>> https://mail.python.org/mailman/listinfo/python-list

[toc] | [prev] | [next] | [standalone]


#102128

FromJoel Goldstick <joel.goldstick@gmail.com>
Date2016-01-26 10:56 -0500
Message-ID<mailman.17.1453823826.2338.python-list@python.org>
In reply to#102111
On Tue, Jan 26, 2016 at 10:48 AM, Tolu Agboola <toluagboola@gmail.com>
wrote:

> I'll go with number 2. I don't know how to apply the ideas to the code I
> have.
>
> Sent from my iPhone
>
> > On 26 Jan 2016, at 15:48, David Palao <dpalao.python@gmail.com> wrote:
> >
> > Well, there is a very specific example in there.
> > But you still don't give us much information about *concrete* troubles
> you have.
> > I *guess* that one of the following is true:
> > 1) You have problems to understand some part of the example in the
> > unittest module documentation.
> > 2) You don't know how to apply those ideas to some specific code you
> have.
> > 3) something else.
> > Could you please be more specific?
> >
> > Best
> >
> > 2016-01-26 13:26 GMT+01:00  <toluagboola@gmail.com>:
> >> Yes I have studied the page but it's quite vague to me. So we have this
> project - Development of a touch screen controllable IEC61850 standard
> protocol analyzer. With this analyzer user can monitor and analyze network
> traffic in substation network.
> >> --
> >> https://mail.python.org/mailman/listinfo/python-list
> --
> https://mail.python.org/mailman/listinfo/python-list
>

Test driven development with python and django.  The complete text is
online here:
http://chimera.labs.oreilly.com/books/1234000000754/pr02.html#_python_3_and_programming


-- 
Joel Goldstick
http://joelgoldstick.com/stats/birthdays

[toc] | [prev] | [next] | [standalone]


#102129

FromDavid Palao <dpalao.python@gmail.com>
Date2016-01-26 17:04 +0100
Message-ID<mailman.18.1453824255.2338.python-list@python.org>
In reply to#102111
2016-01-26 16:56 GMT+01:00 Joel Goldstick <joel.goldstick@gmail.com>:
> On Tue, Jan 26, 2016 at 10:48 AM, Tolu Agboola <toluagboola@gmail.com>
> wrote:
>
>> I'll go with number 2. I don't know how to apply the ideas to the code I
>> have.
>>
>> Sent from my iPhone
>>
>> > On 26 Jan 2016, at 15:48, David Palao <dpalao.python@gmail.com> wrote:
>> >
>> > Well, there is a very specific example in there.
>> > But you still don't give us much information about *concrete* troubles
>> you have.
>> > I *guess* that one of the following is true:
>> > 1) You have problems to understand some part of the example in the
>> > unittest module documentation.
>> > 2) You don't know how to apply those ideas to some specific code you
>> have.
>> > 3) something else.
>> > Could you please be more specific?
>> >
>> > Best
>> >
>> > 2016-01-26 13:26 GMT+01:00  <toluagboola@gmail.com>:
>> >> Yes I have studied the page but it's quite vague to me. So we have this
>> project - Development of a touch screen controllable IEC61850 standard
>> protocol analyzer. With this analyzer user can monitor and analyze network
>> traffic in substation network.
>> >> --
>> >> https://mail.python.org/mailman/listinfo/python-list
>> --
>> https://mail.python.org/mailman/listinfo/python-list
>>
>
> Test driven development with python and django.  The complete text is
> online here:
> http://chimera.labs.oreilly.com/books/1234000000754/pr02.html#_python_3_and_programming
>
>
> --
> Joel Goldstick
> http://joelgoldstick.com/stats/birthdays
> --
> https://mail.python.org/mailman/listinfo/python-list

Good reference.
I like also this one:
http://code.tutsplus.com/tutorials/test-driven-development-in-python--net-30137
which is shorter.

Best

[toc] | [prev] | [next] | [standalone]


#102137

From4ndre4 <4ndre4@4ndre4.com.invalid>
Date2016-01-26 20:57 +0000
Message-ID<n88meg$98e$8@dont-email.me>
In reply to#102109
On 26/01/2016 11:09, toluagboola@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)

[toc] | [prev] | [next] | [standalone]


#102143

Fromtoluagboola@gmail.com
Date2016-01-26 20:57 -0800
Message-ID<eb7a3739-fe65-453f-9df7-552e2d90579d@googlegroups.com>
In reply to#102137
On Tuesday, January 26, 2016 at 10:57:51 PM UTC+2, 4ndre4 wrote:
> On 26/01/2016 11:09, toluagboola@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

[toc] | [prev] | [next] | [standalone]


#102153

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2016-01-27 07:54 -0500
Message-ID<mailman.33.1453899255.2338.python-list@python.org>
In reply to#102143
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/

[toc] | [prev] | [next] | [standalone]


#102194

Fromtoluagboola@gmail.com
Date2016-01-28 08:57 -0800
Message-ID<4183d4d6-bfeb-463f-b26a-656740a18405@googlegroups.com>
In reply to#102143
Thanks for the contributions. Took a while but I got the function done. My problem was in the first two lines, I had to import the python file in my test code which I did not do earlier

[toc] | [prev] | [next] | [standalone]


#102307

From4ndre4 <4ndre4@4ndre4.com.invalid>
Date2016-01-30 18:03 +0000
Message-ID<n8itop$q42$4@dont-email.me>
In reply to#102143
On 27/01/2016 04:57, toluagboola@gmail.com wrote:
[...]
> Here is what the python code looks like and I am to make a Unittest for it

Just a few ideas: first of all, you have to make your code more modular, 
for it to be unit-testable.

There are a few areas in your code that need testing:

- command line option usage/parsing/validation:
   - test that the parameters passed are syntactically correct
   - test that the parameters passed are semantically correct
   - test that the command line usage shown to the user is correct 
(based on the available options)

This might require the creation of a Usage class that encapsulate the 
usage of getopt. The class could validate the input command line, and 
return a string for the usage. As long as you have a Usage class, you 
can test its behaviours.

- filtering/decoding data:
   - test that a filter is correctly set
   - test that the data are correctly decoded

This might require the creation of a Filter and Decoder classes, the 
former encapsulating the behaviour of your 'pc'; the latter 
encapsulating your 'decode'. The dependency between these two classes 
can be mocked, for example. You can mock the data returned by 
pc.datalink() in your decoding code and make sure they are correctly 
decoded.

And so on...

-- 
4ndre4
"The use of COBOL cripples the mind; its teaching should, therefore, be 
regarded as a criminal offense." (E. Dijkstra)

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web