Replacing the way you run test suites helps. Instead of using .main()
add them manually.

I would suggest reading the following article, as it includes methods
to aggregate your test suites together.

http://agiletesting.blogspot.com/2005/01/python-unit-testing-part-1-unittest.html

import sys
sys.argv = sys.argv[5:]

import unittest

class TestDefaultController(unittest.TestCase):

    def testPrintStatement(self):
        print "This line should print"
    def testDatabaseRecordCount(self):
        print "Records in database --- ", db(db.auth_user.id>0).count()

suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestDefaultController))
unittest.TextTestRunner(verbosity=2).run(suite)


python web2py.py -S pms -M -R applications/pms/test/testDefaultController.py
web2py Enterprise Web Framework
Created by Massimo Di Pierro, Copyright 2007-2010
Version 1.75.4 (2010-02-18 14:55:03)
Database drivers available: SQLite3
/home/thadeusb/web2py/applications/pms/modules/utils.py:16:
DeprecationWarning: the md5 module is deprecated; use hashlib instead
  import md5
testDatabaseRecordCount (__builtin__.TestDefaultController) ...
Records in database ---  4052
ok
testPrintStatement (__builtin__.TestDefaultController) ... This line
should print
ok

----------------------------------------------------------------------
Ran 2 tests in 0.006s

OK





-Thadeus





On Wed, Feb 24, 2010 at 12:50 PM, spiffytech <[email protected]> wrote:
> The confusion is not with doctests, but with external unit tests
> created with the 'unittest' module. Could you please post your
> testControllerDefault.py so I can see what you're doing different from
> me?
>
> Please allow me to elaborate on my problems running unit tests with
> web2py. Here is my (very minimal) example unit test file:
> =================
> #!/usr/bin/python
> import unittest
>
> class TestListActiveGames(unittest.TestCase):
>    def testListActiveGames(self):
>        print "This line should print out"
>
> if __name__ == "__main__":
>    unittest.main()
> =================
>
> If the test is actually run, the command line will show "This line
> should print out".
>
> The file is stored at <web2py_root>/applications/api/tests/test.py. My
> current working directory is <web2py_root>.
> I run the following command:
>
> =======
> python web2py.py -S api -M -R applications/api/tests/test.py
> =======
>
> And I get this output:
>
> ================
> web2py Enterprise Web Framework
> Created by Massimo Di Pierro, Copyright 2007-2010
> Version 1.75.4 (2010-02-18 20:57:56)
> Database drivers available: pysqlite
> ================
>
> The output does not contain the line, "This line should print out",
> indicating the unit tests are not being run.
>
> I did some debugging and found that the last two lines, which are
> typical for unittest files, do not work when the file is executed by
> web2py. Rather than being equal to "__main__", which indicates the
> file is being executed from the command line, __name__ is equal to
> "__builtin__". I modified the code to work anyway:
>
> ================
> #!/usr/bin/python
> import unittest
>
> class TestListActiveGames(unittest.TestCase):
>    def testListActiveGames(self):
>        print "This line should print out"
>
> unittest.main()
> ================
>
> Now, I receive this output:
>
> =======
> web2py Enterprise Web Framework
> Created by Massimo Di Pierro, Copyright 2007-2010
> Version 1.75.4 (2010-02-18 20:57:56)
> Database drivers available: pysqlite2
> option -S not recognized
> Usage: web2py.py [options] [test] [...]
>
> Options:
>  -h, --help       Show this message
>  -v, --verbose    Verbose output
>  -q, --quiet      Minimal output
>
> Examples:
>  web2py.py                               - run default set of tests
>  web2py.py MyTestSuite                   - run suite 'MyTestSuite'
>  web2py.py MyTestCase.testSomething      - run
> MyTestCase.testSomething
>  web2py.py MyTestCase                    - run all 'test*' test
> methods
>                                               in MyTestCase
> =======
>
> More debugging revealed that this is output by unittest.main(). The
> unittest module tries to parse arguments on the command line, sees the
> "-S" option that was passed to web2py.py, says "I don't know what '-S'
> means!", and exits.
>
> To coerce unittest.main() to continue running, I made a quick hack to
> shell.py to strip out the extra command line arguments in sys.argv
> before test.py is run:
>
> ==== gluon/shell.py, insert line 165 ====
> sys.argv = sys.argv[5:]
> ===================
>
> I call the test file again, with the same command as before, and get
> this:
>
> ================
> web2py Enterprise Web Framework
> Created by Massimo Di Pierro, Copyright 2007-2010
> Version 1.75.4 (2010-02-18 20:57:56)
> Database drivers available: pysqlite2
>
> ----------------------------------------------------------------------
> Ran 0 tests in 0.000s
>
> OK
> ================
>
> The line, "This line should be printed out", is still not printed.
> Stepping through the code with Python's debugging module, pdb, shows
> that unittest.main() thinks it's being called from web2py.py, and not
> from test.py. unittest.main() looks for tests to run in web2py.py,
> finds none, and quits.
>
> So now I'm stuck: the unittest module expects test.py to be run as a
> standalone script, but instead test.py is being executed within the
> scope of web2py.py, which is confusing the unittest module in several
> ways.
>
>
>
> On Feb 24, 1:02 pm, Thadeus Burgess <[email protected]> wrote:
>> If I create a unittest
>>
>> init/tests/testControllerDefault.py
>>
>> and run the following command line
>>
>> python web2py.py -S <yourapp> -M -R
>> applications/init/tests/testControllerDefault.py
>>
>> My test is ran.
>>
>> I don't see what is so confusing.
>>
>> -Thadeus
>>
>> On Wed, Feb 24, 2010 at 12:01 PM, Thadeus Burgess <[email protected]> 
>> wrote:
>> > consider controllers/default.py
>>
>> > def index():
>> > """
>> >>>> db(db.people.id > 0).count()
>> > '35'
>> > """
>> >    return db(db.people.id > 0).count()
>>
>> > now when I run
>>
>> > `python web2py.py -T init/default/index`
>>
>> > produces my doctest.
>>
>> > -Thadeus
>>
>> > On Wed, Feb 24, 2010 at 2:28 AM, Jon Romero <[email protected]> wrote:
>> >> So, any idea/hint how to do it? Do we need a patch?
>>
>> >> On Feb 23, 10:40 pm, spiffytech <[email protected]> wrote:
>> >>> I'm running the latest version, 1.75.4.
>>
>> >>> On Feb 23, 11:35 am, Thadeus Burgess <[email protected]> wrote:
>>
>> >>> > What version of web2py are you running?
>>
>> >>> > What is the output of ``python web2py.py --help``
>>
>> >>> > -Thadeus
>>
>> >>> > On Tue, Feb 23, 2010 at 5:23 AM, Jon Romero <[email protected]> wrote:
>> >>> > > ust put a prin "Hello world!" inside the test file, everything
>> >>> > > works ok. So, I suppose that this file tries to load AGAIN the web2py
>> >>> > > environment and there is
>> >>> > > some sort of mi
>>
>> >> --
>> >> You received this message because you are subscribed to the Google Groups 
>> >> "web2py-users" group.
>> >> To post to this group, send email to [email protected].
>> >> To unsubscribe from this group, send email to 
>> >> [email protected].
>> >> For more options, visit this group 
>> >> athttp://groups.google.com/group/web2py?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups 
> "web2py-users" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group at 
> http://groups.google.com/group/web2py?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/web2py?hl=en.

Reply via email to