[Twisted-Python] Running unit tests without trial?
Is it possible to run twisted.trial unit tests using python instead of trial, i.e.: "python mytest.py" instead of "trial mytest.py"? 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.
On 06:15 pm, rowen@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
In article <20130925184544.26068.1404349410.divmod.xquotient.2424@top>, exarkun@twistedmatrix.com wrote:
On 06:15 pm, rowen@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
participants (2)
-
exarkun@twistedmatrix.com
-
Russell E. Owen