U e%@sdZddlZddlZddlZddlZddlmZddlmZddlm Z ddl Z e dZ ej dkZdd Zdd d ZGd ddZGdddeZdS)a Attribute selector plugin. Oftentimes when testing you will want to select tests based on criteria rather then simply by filename. For example, you might want to run all tests except for the slow ones. You can do this with the Attribute selector plugin by setting attributes on your test methods. Here is an example: .. code-block:: python def test_big_download(): import urllib # commence slowness... test_big_download.slow = 1 Once you've assigned an attribute ``slow = 1`` you can exclude that test and all other tests having the slow attribute by running :: $ nosetests -a '!slow' There is also a decorator available for you that will set attributes. Here's how to set ``slow=1`` like above with the decorator: .. code-block:: python from nose.plugins.attrib import attr @attr('slow') def test_big_download(): import urllib # commence slowness... And here's how to set an attribute with a specific value: .. code-block:: python from nose.plugins.attrib import attr @attr(speed='slow') def test_big_download(): import urllib # commence slowness... This test could be run with :: $ nosetests -a speed=slow In Python 2.6 and higher, ``@attr`` can be used on a class to set attributes on all its test methods at once. For example: .. code-block:: python from nose.plugins.attrib import attr @attr(speed='slow') class MyTestCase: def test_long_integration(self): pass def test_end_to_end_something(self): pass Below is a reference to the different syntaxes available. Simple syntax ------------- Examples of using the ``-a`` and ``--attr`` options: * ``nosetests -a status=stable`` Only runs tests with attribute "status" having value "stable" * ``nosetests -a priority=2,status=stable`` Runs tests having both attributes and values * ``nosetests -a priority=2 -a slow`` Runs tests that match either attribute * ``nosetests -a tags=http`` If a test's ``tags`` attribute was a list and it contained the value ``http`` then it would be run * ``nosetests -a slow`` Runs tests with the attribute ``slow`` if its value does not equal False (False, [], "", etc...) * ``nosetests -a '!slow'`` Runs tests that do NOT have the attribute ``slow`` or have a ``slow`` attribute that is equal to False **NOTE**: if your shell (like bash) interprets '!' as a special character make sure to put single quotes around it. Expression Evaluation --------------------- Examples using the ``-A`` and ``--eval-attr`` options: * ``nosetests -A "not slow"`` Evaluates the Python expression "not slow" and runs the test if True * ``nosetests -A "(priority > 5) and not slow"`` Evaluates a complex Python expression and runs the test if True N) isfunction)Plugin)tolistznose.plugins.attrib)csfdd}|S)zgDecorator that adds attributes to classes or functions for use with the Attribute (-a) plugin. cs8D]}t||dqD]\}}t|||q|S)NT)setattritems)obnamevalueargskwargsA/opt/hc_python/lib/python3.8/site-packages/nose/plugins/attrib.pywrap_obws zattr..wrap_obr)r rrrr rattrssrFcCs>t}t|||}||kr.|dk r.t|||}||kr:|S|S)zLook up an attribute on a method/ function. If the attribute isn't found there, looking it up in the method's class, if any. N)objectgetattr)methodcls attr_namedefaultMissingr rrrget_method_attrs  rc@s eZdZdZddZddZdS) ContextHelperzObject that can act as context dictionary for eval and looks up names as attributes on a method/ function and its class. cCs||_||_dSN)rrselfrrrrr__init__szContextHelper.__init__cCst|j|j|Sr)rrr)rr rrr __getitem__szContextHelper.__getitem__N)__name__ __module__ __qualname____doc__rr rrrrrsrc@sBeZdZdZddZddZddZdd d Zd d Zd dZ dS)AttributeSelectorz|jr>t|j}|D]}dd}|j||fgq|jrt|j}|D]}g}|dD]d}|srqh|dd} t| dkr| \} } n*| d} | ddkr| dd} d } nd } || | fqh|j|qR|jrd |_ dS) aConfigure the plugin and system, based on selected options. attr and eval_attr may each be lists. self.attribs will be a list of lists of tuples. In that list, each list is a group of attributes, all of which must match for the rule to match. cSst|dt||Sr)evalr)exprobjrrrreval_in_contextsz4AttributeSelector.configure..eval_in_context,=r!NFT) r&r/r,rr'rstripsplitlenenabled) rr2configr,rr6Zstd_attrZ attr_groupZattribrkeyr rrr configures2         zAttributeSelector.configureNc Csd}|jD]}d}|D]\}}t|||}t|tjrL||||sd}qq|dkrft|sd}qq|dkrt|rd}qqt|ttfkrt | dd|Dkrd}qq||krt | t | krd}qq|p|}q |rdSdS)zVerify whether a method has the required attributes The method is considered a match if it matches all attributes for any attribute group. .FTcSsg|]}t|qSr)strlower).0xrrr sz4AttributeSelector.validateAttrib..N) r&r isinstance collectionsCallablebooltypelisttuplerBrC) rrranygroupmatchr@r rrrrvalidateAttribs@      z AttributeSelector.validateAttribcCs ||S)z5Accept the function if its attributes match. )rQ)rfunctionrrr wantFunctionszAttributeSelector.wantFunctioncCs0z |jj}Wntk r"YdSX|||S)z3Accept the method if its attributes match. F)__self__ __class__AttributeErrorrQrrrr wantMethods  zAttributeSelector.wantMethod)N) r!r"r#r$rr2rArQrSrWrrrrr%s5 ,r%)F)r$inspectloggingossysrZnose.plugins.baserZ nose.utilrrH getLoggerlog version_infor/rrrr%rrrrsf