�9�c@s`dZddlZddlZddlmZyddlmZWn!ek
reddlmZnXddgZddl m
Z
defd ��YZydd
l
mZeZWnek
r�eZnXdefd��YZdefd
��YZd�Zd�Zd�Zd�Zd�Zd�Zd�Zedkr\ddlZej�ndS(sn
Testing Plugins
===============
The plugin interface is well-tested enough to safely unit test your
use of its hooks with some level of confidence. However, there is also
a mixin for unittest.TestCase called PluginTester that's designed to
test plugins in their native runtime environment.
Here's a simple example with a do-nothing plugin and a composed suite.
>>> import unittest
>>> from nose.plugins import Plugin, PluginTester
>>> class FooPlugin(Plugin):
... pass
>>> class TestPluginFoo(PluginTester, unittest.TestCase):
... activate = '--with-foo'
... plugins = [FooPlugin()]
... def test_foo(self):
... for line in self.output:
... # i.e. check for patterns
... pass
...
... # or check for a line containing ...
... assert "ValueError" in self.output
... def makeSuite(self):
... class TC(unittest.TestCase):
... def runTest(self):
... raise ValueError("I hate foo")
... return unittest.TestSuite([TC()])
...
>>> res = unittest.TestResult()
>>> case = TestPluginFoo('test_foo')
>>> _ = case(res)
>>> res.errors
[]
>>> res.failures
[]
>>> res.wasSuccessful()
True
>>> res.testsRun
1
And here is a more complex example of testing a plugin that has extra
arguments and reads environment variables.
>>> import unittest, os
>>> from nose.plugins import Plugin, PluginTester
>>> class FancyOutputter(Plugin):
... name = "fancy"
... def configure(self, options, conf):
... Plugin.configure(self, options, conf)
... if not self.enabled:
... return
... self.fanciness = 1
... if options.more_fancy:
... self.fanciness = 2
... if 'EVEN_FANCIER' in self.env:
... self.fanciness = 3
...
... def options(self, parser, env=os.environ):
... self.env = env
... parser.add_option('--more-fancy', action='store_true')
... Plugin.options(self, parser, env=env)
...
... def report(self, stream):
... stream.write("FANCY " * self.fanciness)
...
>>> class TestFancyOutputter(PluginTester, unittest.TestCase):
... activate = '--with-fancy' # enables the plugin
... plugins = [FancyOutputter()]
... args = ['--more-fancy']
... env = {'EVEN_FANCIER': '1'}
...
... def test_fancy_output(self):
... assert "FANCY FANCY FANCY" in self.output, (
... "got: %s" % self.output)
... def makeSuite(self):
... class TC(unittest.TestCase):
... def runTest(self):
... raise ValueError("I hate fancy stuff")
... return unittest.TestSuite([TC()])
...
>>> res = unittest.TestResult()
>>> case = TestFancyOutputter('test_fancy_output')
>>> _ = case(res)
>>> res.errors
[]
>>> res.failures
[]
>>> res.wasSuccessful()
True
>>> res.testsRun
1
i�N(twarn(tStringIOtPluginTestertrun(tgetpidtMultiProcessFilecBsPeZdZd�Zd�Zd�Zd�Zdd�Zd�Zd�Z RS( s\
helper for testing multiprocessing
multiprocessing poses a problem for doctests, since the strategy
of replacing sys.stdout/stderr with file-like objects then
inspecting the results won't work: the child processes will
write to the objects, but the data will not be reflected
in the parent doctest-ing process.
The solution is to create file-like objects which will interact with
multiprocessing in a more desirable way.
All processes can write to this object, but only the creator can read.
This allows the testing system to see a unified picture of I/O.
cCs7t�|_t�j�|_t�|_d|_dS(Ni(Rt_MultiProcessFile__mastertManagertQueuet_MultiProcessFile__queueRt_MultiProcessFile__buffert softspace(tself((s;/sys/lib/python2.7/site-packages/nose/plugins/plugintest.pyt__init__~scCs�t�|jkrdSddlm}ddlm}|t�}x]tr�y|jj �\}}Wn|k
rxPnX|dkr�d}n||c|7<qEWx(t
|�D]}|jj||�q�WdS(Ni�(tEmpty(tdefaultdictg}Ô%��((g}Ô%��(
RRRRtcollectionsRtstrtTrueR t
get_nowaittsortedR
twrite(RRRtcachetpidtdata((s;/sys/lib/python2.7/site-packages/nose/plugins/plugintest.pytbuffer�s
cCs6ddlm}|�j}|jj||f�dS(Ni�(tcurrent_process(tmultiprocessingRt _identityR tput(RRRR((s;/sys/lib/python2.7/site-packages/nose/plugins/plugintest.pyR�scCs|j�|jS(sgetattr doesn't work for iter()(RR
(R((s;/sys/lib/python2.7/site-packages/nose/plugins/plugintest.pyt__iter__�s
icCs|j�|jj||�S(N(RR
tseek(Rtoffsettwhence((s;/sys/lib/python2.7/site-packages/nose/plugins/plugintest.pyR�s
cCs|j�|jj�S(N(RR
tgetvalue(R((s;/sys/lib/python2.7/site-packages/nose/plugins/plugintest.pyR"�s
cCst|j|�S(N(tgetattrR
(Rtattr((s;/sys/lib/python2.7/site-packages/nose/plugins/plugintest.pyt__getattr__�s(
t__name__t
__module__t__doc__R
RRRRR"R%(((s;/sys/lib/python2.7/site-packages/nose/plugins/plugintest.pyRns (RcBsSeZdZdZdZdZiZdZgZ dZ
d�Zd�Zd�Z
RS(s%A mixin for testing nose plugins in their runtime environment.
Subclass this and mix in unittest.TestCase to run integration/functional
tests on your plugin. When setUp() is called, the stub test suite is
executed with your plugin so that during an actual test you can inspect the
artifacts of how your plugin interacted with the stub test suite.
- activate
- the argument to send nosetests to activate the plugin
- suitepath
- if set, this is the path of the suite to test. Otherwise, you
will need to use the hook, makeSuite()
- plugins
- the list of plugins to make available during the run. Note
that this does not mean these plugins will be *enabled* during
the run -- only the plugins enabled by the activate argument
or other settings in argv or env will be enabled.
- args
- a list of arguments to add to the nosetests command, in addition to
the activate argument
- env
- optional dict of environment variables to send nosetests
cCs
t�dS(s�returns a suite object of tests to run (unittest.TestSuite())
If self.suitepath is None, this must be implemented. The returned suite
object will be executed with all plugins activated. It may return
None.
Here is an example of a basic suite object you can return ::
>>> import unittest
>>> class SomeTest(unittest.TestCase):
... def runTest(self):
... raise ValueError("Now do something, plugin!")
...
>>> unittest.TestSuite([SomeTest()]) # doctest: +ELLIPSIS
<unittest...TestSuite tests=[<...SomeTest testMethod=runTest>]>
N(tNotImplementedError(R((s;/sys/lib/python2.7/site-packages/nose/plugins/plugintest.pyt makeSuite�sc Cs�ddlm}ddlm}ddlm}d}t�}|d|jd|d|d|j ��}|j
dk r�|j
|_
n|js�|j�}n|d|j
d |d
|dt�|_t|�|_dS(
s7execute the plugin on the internal test suite.
i�(tConfig(tTestProgram(t
PluginManagertenvtstreamtpluginstargvtconfigtsuitetexitN(tnose.configR+t nose.coreR,tnose.plugins.managerR-tNonetBufferR.R0tignoreFilest suitepathR*R1tFalsetnosetAccessDecoratortoutput(RR+R,R-R3R/tconf((s;/sys/lib/python2.7/site-packages/nose/plugins/plugintest.pyt_execPlugin�s cCs^d|jg|_|jr1|jj|j�n|jrP|jj|j�n|j�dS(sUruns nosetests with the specified test suite, all plugins
activated.
t nosetestsN(tactivateR1targstextendR;tappendRA(R((s;/sys/lib/python2.7/site-packages/nose/plugins/plugintest.pytsetUps N(R&R'R(R8RCR;RDR.R1R0R:R*RARG(((s;/sys/lib/python2.7/site-packages/nose/plugins/plugintest.pyR�s! R>cBs8eZdZdZd�Zd�Zd�Zd�ZRS(cCs6||_|jd�|j�|_|jd�dS(Ni(R/Rtreadt_buf(RR/((s;/sys/lib/python2.7/site-packages/nose/plugins/plugintest.pyR
s
cCs
||jkS(N(RI(Rtval((s;/sys/lib/python2.7/site-packages/nose/plugins/plugintest.pyt__contains__scCs
t|j�S(N(titerR/(R((s;/sys/lib/python2.7/site-packages/nose/plugins/plugintest.pyRscCs|jS(N(RI(R((s;/sys/lib/python2.7/site-packages/nose/plugins/plugintest.pyt__str__sN( R&R'R8R/RIR
RKRRM(((s;/sys/lib/python2.7/site-packages/nose/plugins/plugintest.pyR>s ccs�g}xm|jt�D]\}|j|�|j�}|s[|jd�r|jd�rdj|�Vg}qqW|r�dj|�VndS(s9a bunch of === characters is also considered a blank lines===t=tN(t
splitlinesRRFtstript
startswithtjoin(ttexttblocktline((s;/sys/lib/python2.7/site-packages/nose/plugins/plugintest.pytblankline_separated_blocks#s
&
cCsftjdtjtjBtjB�}g}x-t|�D]}|j|jd|��q6Wdj|�S(NsD
# Grab the traceback header. Different versions of Python have
# said different things on the first traceback line.
^(?P<hdr> Traceback\ \(
(?: most\ recent\ call\ last
| innermost\ last
) \) :
)
\s* $ # toss trailing whitespace on the header.
(?P<stack> .*?) # don't blink: absorb stuff until...
^(?=\w) # a line *starts* with alphanum.
.*?(?P<exception> \w+ ) # exception name
(?P<msg> [:\n] .*) # the rest
s"\g<hdr>\n...\n\g<exception>\g<msg>RO( tretcompiletVERBOSEt MULTILINEtDOTALLRWRFtsubRS(toutttraceback_retblocksRU((s;/sys/lib/python2.7/site-packages/nose/plugins/plugintest.pytremove_stack_traces0s
cCs,tjdtjtjB�}|jd|�S(Ns�
# Cut the file and line no, up to the warning name
^.*:\d+:\s
(?P<category>\w+): \s+ # warning category
(?P<detail>.+) $ \n? # warning message
^ .* $ # stack frame
s\g<category>: \g<detail>(RXRYRZR[R](R^twarn_re((s;/sys/lib/python2.7/site-packages/nose/plugins/plugintest.pytsimplify_warningsFscCstjdd|�S(NsRan (\d+ tests?) in [0-9.]+ssRan \1 in ...s(RXR](R^((s;/sys/lib/python2.7/site-packages/nose/plugins/plugintest.pytremove_timingsQscCs.t|�}t|�}t|�}|j�S(s6Modify nose output to make it easy to use in doctests.(RaRcRdRQ(R^((s;/sys/lib/python2.7/site-packages/nose/plugins/plugintest.pytmunge_nose_output_for_doctestVscOssddlm}ddlm}ddlm}t�}d|kr�|jdg�}t|t �rx|d|�}n|jdi�}|d|d|�|d<nd|kr�d d
g|d<n||d_
tj}tj
} |jdt�r|t_
t_t}
nt}
tdtd
d�z|||�Wd|
rW|t_| t_
nX|j�}t|�GHdS(s�
Specialized version of nose.run for use inside of doctests that
test test runs.
This version of run() prints the result output to stdout. Before
printing, the output is processed by replacing the timing
information with an ellipsis (...), removing traceback stacks, and
removing trailing whitespace.
Use this version of run wherever you are writing a doctest that
tests nose (or unittest) test result output.
Note: do not use doctest: +ELLIPSIS when testing nose output,
since ellipses ("test_foo ... ok") in your expected test runner
output may match multiple lines of output, causing spurious test
passes!
i�(R(R+(R-R2R0R.R1RBs-vt
buffer_allsThe behavior of nose.plugins.plugintest.run() will change in the next release of nose. The current behavior does not correctly account for output to stdout and stderr. To enable correct behavior, use run_buffered() instead, or pass the keyword argument buffer_all=True to run().t
stackleveliN(R=RR5R+R7R-R9tpopt
isinstancetlistR/tsyststderrtstdoutR<RRtDeprecationWarningR"Re(targtkwRR+R-RR0R.RlRmtrestoreR^((s;/sys/lib/python2.7/site-packages/nose/plugins/plugintest.pyR^s8
cOst|d<t||�dS(NRf(RR(RoRp((s;/sys/lib/python2.7/site-packages/nose/plugins/plugintest.pytrun_buffered�s
t__main__(R(RXRktwarningsRt cStringIORtImportErrort__all__tosRtobjectRRRR9RR>RWRaRcRdReRRrR&tdoctestttestmod(((s;/sys/lib/python2.7/site-packages/nose/plugins/plugintest.pyt<module>`s6
?
`
<
|