Current File : //usr/lib/python2.7/site-packages/nose/plugins/plugintest.pyc |
�
d�Uc @ s` d Z d d l Z d d l Z d d l m Z y d d l m Z Wn! e k
re d d l m Z n Xd d g Z d d l m
Z
d e f d � � YZ y d d
l
m Z e Z Wn e k
r� e Z n Xd e f d � � YZ d e f d
� � YZ d � Z d � Z d � Z d � Z d � Z d � Z d � Z e d k r\d d l Z e j � n d S( sX
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 [TC('runTest')]
...
>>> 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 [TC('runTest')]
...
>>> res = unittest.TestResult()
>>> case = TestFancyOutputter('test_fancy_output')
>>> _ = case(res)
>>> res.errors
[]
>>> res.failures
[]
>>> res.wasSuccessful()
True
>>> res.testsRun
1
i����N( t warn( t StringIOt PluginTestert run( t getpidt MultiProcessFilec B sP e Z d Z d � Z d � Z d � Z d � Z d d � Z d � Z d � 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.
c C s7 t � | _ t � j � | _ t � | _ d | _ d S( Ni ( R t _MultiProcessFile__mastert Managert Queuet _MultiProcessFile__queueR t _MultiProcessFile__buffert softspace( t self( ( s; /usr/lib/python2.7/site-packages/nose/plugins/plugintest.pyt __init__~ s c C s� t � | j k r d Sd d l m } d d l m } | t � } x] t r� y | j j � \ } } Wn | k
rx Pn X| d k r� d } n | | c | 7<qE Wx( t
| � D] } | j j | | � q� Wd S( Ni����( t Empty( t defaultdictg}Ô%�I�T( ( g}Ô%�I�T(
R R R R t collectionsR t strt TrueR t
get_nowaitt sortedR
t write( R R R t cachet pidt data( ( s; /usr/lib/python2.7/site-packages/nose/plugins/plugintest.pyt buffer� s
c C s6 d d l m } | � j } | j j | | f � d S( Ni����( t current_process( t multiprocessingR t _identityR t put( R R R R ( ( s; /usr/lib/python2.7/site-packages/nose/plugins/plugintest.pyR � s c C s | j � | j S( s getattr doesn't work for iter()( R R
( R ( ( s; /usr/lib/python2.7/site-packages/nose/plugins/plugintest.pyt __iter__� s
i c C s | j � | j j | | � S( N( R R
t seek( R t offsett whence( ( s; /usr/lib/python2.7/site-packages/nose/plugins/plugintest.pyR � s
c C s | j � | j j � S( N( R R
t getvalue( R ( ( s; /usr/lib/python2.7/site-packages/nose/plugins/plugintest.pyR"