On 06:15 pm, ro...@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()

Or you can just invoke the same code that the command line trial script invokes:

   from twisted.scripts.trial import run
   run()

(That's not exactly the same as /usr/bin/trial, but you can read /usr/bin/trial to see the differences if you're interested).

Jean-Paul

I'm hoping there is some simple magic I can put into the unit test
itself to make this work. Perhaps something in
if __name__ == "__main__":
  ...?
to make it start up the reactor and run it correctly.

I ask because I'm using a build environment that runs all unit tests
this way. It does the sniffing out and the saving of logs, but it
insists on running .py tests using python.

-- Russell

P.S. being *able* to run trial unit tests using python seems like a
feature to me in any case -- one I'd be willing to add extra code to
support. Trial has some nice features, but I find it confusing that it
is *required* to run a single test with a ".py" extension when visually
it looks like a python file.

For what it's worth, just having a ".py" extension doesn't mean a file is intended to be run directly. In fact, the vast majority of ".py" files are not intended to be run directly. For example, out of the 816 ".py" files that are part of Twisted, probably only a handful are meant to be run directly.

What the ".py" extension unquestionably means is "this is a python module, it can be imported". Arguably command line entrypoints should not have a ".py" extension - just as command line tools written in C and compiled to some version of native don't have a ".a" or ".so" extension (let alone a ".c" extension).

Jean-Paul

_______________________________________________
Twisted-Python mailing list
Twisted-Python@twistedmatrix.com
http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python

Reply via email to