[web2py] Re: How do you organize your applications to be unit-tested?

2012-10-04 Thread monotasker
One thing I'm trying out right now is (a) writing all of my business logic 
in module files; (b) breaking up that logic into small-ish classes that 
each seem to have with around 5-10 methods each; (c) injecting my 
dependencies as much as possible into the class in the __init__ method and 
then passing them from __init__ to the class methods. This seems to make it 
much easier to mock external dependencies. So, for example, rather than 
performing a db select in a method, I might do something like this:

class Myclass(object):

def __init__(self, myrows=None, other_class_instance=None):
if myrows is None:
db = current.db
myrows = db(db.mytable.id > 0).select()
self.myrows = myrows

if other_class_instance is None:
other_class_instance = OtherClassInstance()
self.other_class_instance = other_class_instance

def my_method(self, target)

person = self.myrows.find(lambda row: row.id ==  target)
bday = self.other_class_instance.get_birthday()

if person.birthday = bday
return 'Happy Birthday!'
else:
return 'No cake for you!'

In order to test this class all I have to provide is something that looks 
like a Rows object and something that looks like an instance of another 
class. If I pass those mocked objects at class instantiation, I override 
the default calls to db and the external class in __init__() and my class 
is effectively decoupled for testing. I can even run tests on this class 
(should I want to) without the web2py environment.

I should add that I've just been reading a lot about tdd and testing in 
general, so there's a good chance I'm passing on flawed advice here. :) But 
it's working for me so far. The dependency injection idea can be taken to 
extremes, with decorators and whole "frameworks" but for my purposes the 
simple approach taken here seems functional. The other advantage of this is 
that your classes and methods become much more abstract and so much easier 
to reuse in other projects. Or if (perish the thought) you someday wanted 
to refactor your app using Django or something you could use a class like 
this largely untouched, as long as you pass an alternative for db in to the 
class constructor.

Cheers,

Ian

On Wednesday, October 3, 2012 3:46:03 PM UTC-4, viniciusban wrote:
>
> Hi all. 
>
> I continue diving into unit-testing and I'd like to know how do you 
> organize your applications to be unit-tested. 
>
> Thin controllers? DAO classes? How do you decouple things in web2py to 
> make unit-testing easy? 
>
> -- 
> Vinicius Assef 
>

-- 





Re: [web2py] Re: How do you organize your applications to be unit-tested?

2012-10-04 Thread Richard Vézina
I think that with web2py there is no unique fit all solution, and doctest
is not that usefull from my point of view, or it should be use with .txt
file and not be include in docstring, to make sure it is not executed with
the regular code.

Also, I really not sure that unit testing a simple web2py function is very
usefull, except for the really complexe one. I mean what can provide a unit
test when you just render a simple form with SQLFORM... It differents for
the more complexe (customized form or decoupled form from the db model),
but it is not clear to me what I should test and not test.

In the pass and for my needs (and the time I had for testing then), I wrote
a bunch of tests in Selenium that load every pages and simply check if the
page load with no error. I want to go further in unit testing and TDD, but
I have a lot of things to learn.

unittest appears to me much more convenient for structuring the test base
then docstring.

Richard

On Thu, Oct 4, 2012 at 12:04 PM, vinicius...@gmail.com <
vinicius...@gmail.com> wrote:

> You're right, Richard, but I was asking how you organize your production
> code, not your test code.
>
> What is your strategies to isolate components to achieve a better
> unit-testing scenario?
>
> --
> Vinicius Assef
>
>
>
> On 10/04/2012 12:49 PM, Richard Vézina wrote:
>
>> I am reading actually about unit testing and doctest in docstring should
>> be pretty small, as a rule of thumb it should be less longer than you
>> docstring documentation.
>>
>> But, it says too that it pretty convenient to have at least one doctest
>> in docstring since it could help a lot in identifying outdated docstring
>> since if you refactor you code without touch you docstring your doctest
>> should failed so you know you are good for a bit of documentation
>> pleasure.
>>
>> I read too that docstring can be kept apart the docstring a .txt file,
>> so the long unit test should go there.
>>
>> So when doctest help you better understand and document the purpose of
>> the code and if it is short it should go in docstring if it get bloated
>> you can move it in .txt file apart.
>>
>> Richard
>>
>> On Thu, Oct 4, 2012 at 10:27 AM, Marek Mollin > > wrote:
>>
>> I only use doc-string testing. For everything I do its snap and
>> effective enough. Plus it can trigered from admin.
>> Though it is not recomnded way of unittesting - it has its problems,
>> bloats the code base, does not provide clean seperation between
>> tests and actual app.
>>
>>
>> W dniu środa, 3 października 2012 21:46:03 UTC+2 użytkownik
>> viniciusban napisał:
>>
>> Hi all.
>>
>> I continue diving into unit-testing and I'd like to know how do
>> you
>> organize your applications to be unit-tested.
>>
>> Thin controllers? DAO classes? How do you decouple things in
>> web2py to
>> make unit-testing easy?
>>
>> --
>> Vinicius Assef
>>
>> --
>>
>>
>>
>>
>> --
>>
>>
>>
>>
> --
>
>
>
>

-- 





Re: [web2py] Re: How do you organize your applications to be unit-tested?

2012-10-04 Thread vinicius...@gmail.com
You're right, Richard, but I was asking how you organize your production 
code, not your test code.


What is your strategies to isolate components to achieve a better 
unit-testing scenario?


--
Vinicius Assef


On 10/04/2012 12:49 PM, Richard Vézina wrote:

I am reading actually about unit testing and doctest in docstring should
be pretty small, as a rule of thumb it should be less longer than you
docstring documentation.

But, it says too that it pretty convenient to have at least one doctest
in docstring since it could help a lot in identifying outdated docstring
since if you refactor you code without touch you docstring your doctest
should failed so you know you are good for a bit of documentation pleasure.

I read too that docstring can be kept apart the docstring a .txt file,
so the long unit test should go there.

So when doctest help you better understand and document the purpose of
the code and if it is short it should go in docstring if it get bloated
you can move it in .txt file apart.

Richard

On Thu, Oct 4, 2012 at 10:27 AM, Marek Mollin mailto:rog...@gmail.com>> wrote:

I only use doc-string testing. For everything I do its snap and
effective enough. Plus it can trigered from admin.
Though it is not recomnded way of unittesting - it has its problems,
bloats the code base, does not provide clean seperation between
tests and actual app.


W dniu środa, 3 października 2012 21:46:03 UTC+2 użytkownik
viniciusban napisał:

Hi all.

I continue diving into unit-testing and I'd like to know how do you
organize your applications to be unit-tested.

Thin controllers? DAO classes? How do you decouple things in
web2py to
make unit-testing easy?

--
Vinicius Assef

--




--





--





Re: [web2py] Re: How do you organize your applications to be unit-tested?

2012-10-04 Thread Richard Vézina
I am reading actually about unit testing and doctest in docstring should be
pretty small, as a rule of thumb it should be less longer than you
docstring documentation.

But, it says too that it pretty convenient to have at least one doctest in
docstring since it could help a lot in identifying outdated docstring since
if you refactor you code without touch you docstring your doctest should
failed so you know you are good for a bit of documentation pleasure.

I read too that docstring can be kept apart the docstring a .txt file, so
the long unit test should go there.

So when doctest help you better understand and document the purpose of the
code and if it is short it should go in docstring if it get bloated you can
move it in .txt file apart.

Richard

On Thu, Oct 4, 2012 at 10:27 AM, Marek Mollin  wrote:

> I only use doc-string testing. For everything I do its snap and effective
> enough. Plus it can trigered from admin.
> Though it is not recomnded way of unittesting - it has its problems,
> bloats the code base, does not provide clean seperation between tests and
> actual app.
>
>
> W dniu środa, 3 października 2012 21:46:03 UTC+2 użytkownik viniciusban
> napisał:
>
>> Hi all.
>>
>> I continue diving into unit-testing and I'd like to know how do you
>> organize your applications to be unit-tested.
>>
>> Thin controllers? DAO classes? How do you decouple things in web2py to
>> make unit-testing easy?
>>
>> --
>> Vinicius Assef
>>
>  --
>
>
>
>

-- 





[web2py] Re: How do you organize your applications to be unit-tested?

2012-10-04 Thread Marek Mollin
I only use doc-string testing. For everything I do its snap and effective 
enough. Plus it can trigered from admin.
Though it is not recomnded way of unittesting - it has its problems, bloats 
the code base, does not provide clean seperation between tests and actual 
app.


W dniu środa, 3 października 2012 21:46:03 UTC+2 użytkownik viniciusban 
napisał:
>
> Hi all. 
>
> I continue diving into unit-testing and I'd like to know how do you 
> organize your applications to be unit-tested. 
>
> Thin controllers? DAO classes? How do you decouple things in web2py to 
> make unit-testing easy? 
>
> -- 
> Vinicius Assef 
>

--