[Grok-dev] Stuck on unit/integration testing

Uli Fouquet uli at gnufix.de
Thu Feb 21 20:53:52 EST 2008


Hi Peter,

please apologize the length of this mail. I am sure only a few points
will touch your problem, but it might be important for others.

Peter Bengtsson wrote:
> I've setup z3c.testsetup into my buildout and I can run ./bin/test.
> In PeterbeLifeBlog/src/peterbelifeblog/tests.py I have this code::
> 
> import z3c.testsetup
> test_suite = z3c.testsetup.register_all_tests('peterbelifeblog.app_tests')
> 
> And in PeterbeLifeBlog/src/peterbelifeblog/app_tests/test_something.py
> I have the following code::
> 
> """
> Tests with real TestCase objects.
> 
> :Test-Layer: python
> 
> """
> import unittest
> class TestTest(unittest.TestCase):
> 
>      def setUp(self):
>          pass
> 
>      def testFoo(self):
>          self.assertEqual(2, 1+1)
> 
> 
> So far so good. How do I get started from here to write unit tests and 
> functional tests?

I assume, the test is executed when you start the test runner? That's
fine. If not, please check your buildout.cfg. The 'tests-pattern' in
defaults must match your modulename (here: 'tests', the modulename of a
file 'tests.py').

> I want to import my application class and some other stuff from app.py 
> and run normal unit tests and I also want to write unit tests with Grok 
> up and running with an instance object in ZODB like I'm able to do in 
> zope2's ZopeTestCase with the self.app thing.
> 
> It feels like it's right there in front of my but I don't know how to 
> get started.

I'm not sure where exactly the problem is, therefore I might give some
hints for some different questions:

How to create testfiles
-----------------------

For unit doctests:

  In your `app_tests` directory create a file `app_unittests.txt` and 
  in there write something like this::

    My Unit Tests For app
    *********************

    :Test-Layer: unit

    We start with a simple problem::

       >>> 1+1
       2

    Hope that does not fail.

For functional doctests:

   Do the same (with a different file), but use 
   `:Test-Layer: functional` as marker string.

Run bin/test and the tests should be executed.


How to write tests in Zope3 environments generally
--------------------------------------------------

The difference between both kinds of tests, unit doctests and functional
doctests is somewhat complex to explain. For short: in functional
doctests you get a whole framework environment running during tests.
This enables you, for instance, to write browser tests, were you can
send simulated requests to a server instance and check the results. You
can also make nasty things with the ZODB.

Unfortunately, all this goes more into _writing_ of tests, while
z3c.testsetup tries only to ease the _setup_ of those. I personally
learned most about writing tests in Zope 3 from 

 - Philipps book, 

 - reading tests of other packages and, of course, 

 - the documentation of the testing packages. Namely 

     * zope.testing,

     * zope.app.testing and 

     * zope.testbrowser. 

   See all those READMEs and other .txt files in that packages. You can 
   browse them conveniently via

     http://apidoc.zope.org/++apidoc++/

   or for a special package:

     http://apidoc.zope.org/++apidoc++/Code/<pkg_name_sep_by_slashes>

   For example:

          http://apidoc.zope.org/++apidoc++/Code/zope/app/testing

   for the `zope.app.testing` package. There you can watch the .txt 
   files rendered nicely to HTML.
  
   The docgrok doc browser works similar, but lists also packages that 
   are not in the 'zope' namespace. Unfortunately it is broken for 
   'zope' packages in the 0.11 version (fixed in the trunk).

I understand, that some special introduction to this topic is still
missing (beside Philipps book, which I would recommend warmly). Please
give some time for that. After having the basic issues fixed, I will try
to give some more general introduction to testing on the grok website.


How to test my app
------------------

This is a more specific question than how to write tests in general, but
might be your real problem here. I cannot explain every dirty detail of
that here, but to grab your application in tests you can do something
like this:

- in unit doctests:

    Here you normally create instances of your app and check things, 
    that do not require browser-based interaction::

       >>> from peterbelifeblog.app import Cave
       >>> cave = Cave()

     Then you can run your tests::

       >>> len(cave.things)
       0

       >>> cave.poulate('club')
       >>> len(cave.things)
       1

- in functional doctests:

    Here you can also create an instance of your app by doing simulated 
    browser requests::

      >>> from zope.testbrowser.testing import Browser
      >>> browser = Browser()
      >>> browser.addHeader('Authorization', 'Basic mgr:mgrpw')

    We fetch the standard page, which should provide us a menu to get 
    all installable grok applications/components.

      >>> browser.open("http://localhost/")
      >>> print browser.contents
      <html xmlns="http://www.w3.org/1999/xhtml">
      ...
      ...      <legend>Add application</legend>
      ...

    We are able to add a mammoth manager...

      >>> subform = browser.getForm(name='MammothManager')
      >>> subform.getControl(
      ....    'Name your new app:').value = 'my-mammoth-manager'
      >>> subform.getControl('Create').click()

      >>> print browser.contents
      <html xmlns="http://www.w3.org/1999/xhtml">
      ...
      ...<legend>Installed applications</legend>
      ...
      ...<a href="http://localhost/my-mammoth-manager">
      ...

   etc. You get the idea. 

   To get the root folder of your testing environment, you can use the 

      zope.app.testing.functional.getRootFolder()

   function. With z3c.testsetup this is already imported as a global, 
   so that you can simply write (without prior imports)::

      >>> root = getRootFolder()
      >>> root['my-mammoth-manager']
      <MammothManager object at 0x...>

   or similar.



> Besides, in my test_something.py I tried to do this:
> from peterbelifeblog.app import SomeClass
> but I keep getting ImportErrors even though the folder 'peterbelifeblog' 
> is in sys.path.

This is really strange. Is it a grokproject? Would you mind giving me a
view on the complete project/setup (via private mail)? At least a
complete failure log (traceback or testrunner output when run with -v
option) would help.

Kind regards,

-- 
Uli




More information about the Grok-dev mailing list