[Twisted-Python] Running unit tests without trial?

Russell E. Owen rowen at uw.edu
Wed Sep 25 16:40:16 MDT 2013


In article <20130925184544.26068.1404349410.divmod.xquotient.2424 at top>,
 exarkun at twistedmatrix.com wrote:

> On 06:15 pm, rowen at uw.edu wrote:
> >Is it possible to run twisted.trial unit tests using python instead of
> >trial, i.e.: "python mytest.py" instead of  "trial mytest.py"?
> 
> Sure.  Here's the worst case answer (contents of mytest.py):
> 
>     import os
>     os.system("trial myrealtests.py")
> 
> By the way, it's usually better to use the FQPN to run tests - not the 
> filename.  Using the filename exposes you to all kinds of gross edge 
> cases that trial doesn't always handle very well (having to do with how 
> modules are loaded in Python).  So consider this instead:
> 
>     trial mytest
> 
> Or better yet, since you should be distributing and installing your test 
> suite along with your package:
> 
>     trial myproject.test
> 
> There *are* better ways than `os.system´ to get a runnable Python source 
> file that will run your tests though.
> 
> One that shouldn't be much of a surprise is to do exactly what you would 
> do for a stdlib unittest-based test module:
> 
>     if __name__ == '__main__':
>         import unittest
>         unittest.main()

I am surprised it works because I had already tried:

from twisted.trial import unittest
...
if __name__ == '__main__':
    twisted.trial.unittest.main()

but twisted.trial.unittest has no main function (a rude surprise), so it 
never occurred to me that the built-in unittest library's main would 
work.

It seems to be exactly what I want.

> Or you can just invoke the same code that the command line trial script 
> invokes:
> 
>     from twisted.scripts.trial import run
>     run()

I tried that as well, but as you say, it is acting as if it was started 
by trial, so it prints help and quits. I guess it wants some arguments.  
I'm sure it'd be useful for some use cases, but for mine your first 
suggestion looks perfect.

Many thanks.

-- Russell




More information about the Twisted-Python mailing list