ɺhH<xdZdZddlZddlZddlZddlZddlZ ddlmZ n#e $rddl Z YnwxYwej dZ ejjZeZeZdZdZdZdZGd d eZd Zd Zd ZdZdZGddeZdZ Gddej!eZ!dZ"dS)aAdds support for parameterized tests to Python's unittest TestCase class. A parameterized test is a method in a test case that is invoked with different argument tuples. A simple example: class AdditionExample(_parameterized.TestCase): @_parameterized.parameters( (1, 2, 3), (4, 5, 9), (1, 1, 3)) def testAddition(self, op1, op2, result): self.assertEqual(result, op1 + op2) Each invocation is a separate test case and properly isolated just like a normal test method, with its own setUp/tearDown cycle. In the example above, there are three separate testcases, one of which will fail due to an assertion error (1 + 1 != 3). Parameters for individual test cases can be tuples (with positional parameters) or dictionaries (with named parameters): class AdditionExample(_parameterized.TestCase): @_parameterized.parameters( {'op1': 1, 'op2': 2, 'result': 3}, {'op1': 4, 'op2': 5, 'result': 9}, ) def testAddition(self, op1, op2, result): self.assertEqual(result, op1 + op2) If a parameterized test fails, the error message will show the original test name (which is modified internally) and the arguments for the specific invocation, which are part of the string returned by the shortDescription() method on test cases. The id method of the test, used internally by the unittest framework, is also modified to show the arguments. To make sure that test names stay the same across several invocations, object representations like >>> class Foo(object): ... pass >>> repr(Foo()) '<__main__.Foo object at 0x23d8610>' are turned into '<__main__.Foo>'. For even more descriptive names, especially in test logs, you can use the named_parameters decorator. In this case, only tuples are supported, and the first parameters has to be a string (or an object that returns an apt name when converted via str()): class NamedExample(_parameterized.TestCase): @_parameterized.named_parameters( ('Normal', 'aa', 'aaa', True), ('EmptyPrefix', '', 'abc', True), ('BothEmpty', '', '', True)) def testStartsWith(self, prefix, string, result): self.assertEqual(result, strings.startswith(prefix)) Named tests also have the benefit that they can be run individually from the command line: $ testmodule.py NamedExample.testStartsWithNormal . -------------------------------------------------------------------- Ran 1 test in 0.000s OK Parameterized Classes ===================== If invocation arguments are shared across test methods in a single TestCase class, instead of decorating all test methods individually, the class itself can be decorated: @_parameterized.parameters( (1, 2, 3) (4, 5, 9)) class ArithmeticTest(_parameterized.TestCase): def testAdd(self, arg1, arg2, result): self.assertEqual(arg1 + arg2, result) def testSubtract(self, arg2, arg2, result): self.assertEqual(result - arg1, arg2) Inputs from Iterables ===================== If parameters should be shared across several test cases, or are dynamically created from other sources, a single non-tuple iterable can be passed into the decorator. This iterable will be used to obtain the test cases: class AdditionExample(_parameterized.TestCase): @_parameterized.parameters( c.op1, c.op2, c.result for c in testcases ) def testAddition(self, op1, op2, result): self.assertEqual(result, op1 + op2) Single-Argument Test Methods ============================ If a test method takes only one argument, the single argument does not need to be wrapped into a tuple: class NegativeNumberExample(_parameterized.TestCase): @_parameterized.parameters( -1, -3, -4, -5 ) def testIsNegative(self, arg): self.assertTrue(IsNegative(arg)) z!tmarek@google.com (Torsten Marek)Nz0\<([a-zA-Z0-9_\-\.]+) object at 0x[a-fA-F0-9]+\>cRtdt|S)Nz<\1>)ADDR_REsubreprobjs /builddir/build/BUILD/imunify360-venv-2.5.2/opt/imunify360/venv/lib64/python3.11/site-packages/google/protobuf/internal/_parameterized.py _CleanReprr s Wd3ii ( ((c$|jd|jS)N.) __module____name__)clss r _StrClassrsNNNCLL 11r cbt|tjot|t SN) isinstancecollections_abcIterablestrrs r _NonStringIterablers, S/2 3 3 #c"" "$r c&t|tjr1dd|DSt |r(dt t|St|fS)Nz, c3DK|]\}}|dt|VdS)=N)r ).0argnamevalues r z'_FormatParameterList..sVDD'%!(E):):):;DDDDDDr ) rrMappingjoinitemsrmapr _FormatParameterList)testcase_paramss r r$r$s!8994 99DD+:+@+@+B+BDDD D DD/**4 99S_55 6 66  2 3 33r c$eZdZdZdZdZdZdS)_ParameterizedTestIterz9Callable and iterable class for producing new test cases.c0||_||_||_dS)a\Returns concrete test functions for a test and a list of parameters. The naming_type is used to determine the name of the concrete functions as reported by the unittest framework. If naming_type is _FIRST_ARG, the testcases must be tuples, and the first element must have a string representation that is a valid Python identifier. Args: test_method: The decorated test method. testcases: (list of tuple/dict) A list of parameter tuples/dicts for individual test invocations. naming_type: The test naming type, either _NAMED or _ARGUMENT_REPR. N) _test_method testcases _naming_type)self test_methodr* naming_types r __init__z_ParameterizedTestIter.__init__s!$DDN#Dr c td)NzYou appear to be running a parameterized test case without having inherited from parameterized.TestCase. This is bad because none of your test cases are actually being run.) RuntimeError)r,argskwargss r __call__z_ParameterizedTestIter.__call__s A B BBr cV|j|jfdfd|jDS)Nctjfd}tur5d|_|xjt dz c_ddn4t urdtd|_ntd|jdtd|_ j r|xj dj z c_ |S) Ncttjr |fidStr |gRdS|dSr)rrr r)r,r-r%s r BoundParamTestzS_ParameterizedTestIter.__iter__..MakeBoundParamTest..BoundParamTests| o'> ? ? - +d . .o . . . . .  0 0 - +d -_ - - - - - - +dO , , , , ,r Tr()z is not a valid naming type. ) functoolswraps _FIRST_ARG__x_use_name__rr_ARGUMENT_REPRr$__x_extra_id__r1__doc__)r%r8r.r-s` r MakeBoundParamTestz;_ParameterizedTestIter.__iter__..MakeBoundParamTests {##-----$#-  " ")-%3q'9#:#::)!""- . ( ( ( ! 1 1 1 1)4%%{{{LMMM  ! ! !#7#H#H#H#H Jn  BK,?,?"AA r c3.K|]}|VdSr)rcrDs r rz2_ParameterizedTestIter.__iter__..s/ : :a  q ! ! : : : : : :r )r)r+r*)r,rDr.r-s @@@r __iter__z_ParameterizedTestIter.__iter__sQ#K#K> ; : : :4> : : ::r N)rr __qualname__rCr/r4rHrFr r r'r'sJAA$$$$BBB #;#;#;#;#;r r'c`t|dkot|dt S)z._Apply#si#tA !+I7O!P!P $y///   j #CK @ @@r rz7Single parameter argument must be a non-string iterable)rNr)r.r*rhs`` r _ParameterDecoratorrisu A A A A A Ai   il + +CCACC +! I -r c,tt|S)aiA decorator for creating parameterized tests. See the module docstring for a usage example. Args: *testcases: Parameters for the decorated method, either a single iterable, or a list of tuples/dicts/objects (for tests with only one argument). Returns: A test generator to be handled by TestGeneratorMetaclass. )rirArMs r parametersrk6s ^Y 7 77r c,tt|S)aA decorator for creating parameterized tests. See the module docstring for a usage example. The first element of each parameter tuple should be a string and will be appended to the name of the test method. Args: *testcases: Parameters for the decorated method, either a single iterable, or a list of tuples. Returns: A test generator to be handled by TestGeneratorMetaclass. )rir?rMs r named_parametersrmEs Z 3 33r ceZdZdZdZdS)TestGeneratorMetaclassaMetaclass for test cases with test generators. A test generator is an iterable in a testcase that produces callables. These callables must be single-argument methods. These methods are injected into the class namespace and the original iterable is removed. If the name of the iterable conforms to the test pattern, the injected methods will be picked up as tests by the unittest framework. In general, it is supposed to be used in conjunction with the parameters decorator. ctix|d<}|D]n\}}|tjjrEt |r6t|}||t||||ot ||||S)NrP) rSr"rTrUrVrWriterpopr[re__new__)mcs class_namebasesdctr^r_riterators r rszTestGeneratorMetaclass.__new__cs$&&C  XXZZ%%''II c //(-> ? ?I S ! !I99  (ixHHH <<Z 4 44r N)rrrIrCrsrFr r roroVs-   5 5 5 5 5r roc t|D]r\}}t|s Jd|t|ddr|j}n d|t|fz}||vsJd|d|||<t|dd||<sd S) aAdds individual test cases to a dictionary. Args: dct: The target dictionary. id_suffix: The dictionary for mapping names to test IDs. name: The original name of the test case. iterator: The iterator generating the individual test cases. z*Test generators must yield callables, got r@Fz%s%s%dz!Name of parameterized test case "z " not uniquerBN) enumeratecallablerQr _SEPARATOR)rwr^r_rxidxfuncnew_names r r[r[osX&& > >ic4 D>> >t%u--4hhT:s33h 3    =EXXG   CM!$(8"==Ih > >r c$eZdZdZdZdZdZdS)TestCasez9Base class for test cases using the parameters decorator.cL|jtdS)Nr)_testMethodNamesplitr}r,s r _OriginalNamezTestCase._OriginalNames   % %j 1 1! 44r cZ|dt|jdS)Nz (r;)rr __class__rs r __str__zTestCase.__str__s/**,,,,i.G.G.G.G HHr ct|jd||j|jdS)zReturns the descriptive ID of the test. This is used internally by the unittesting framework to get a name for the test to be used in reports. Returns: The test id. r rz)rrrrPgetrrs r idz TestCase.idsP"$.1111**,,,++D,@"EEE GGr N)rrrIrCrrrrFr r rrsOAA555III G G G G Gr r) metaclasscdtd|jtfi}|d|tfiS)a$Returns a new base class with a cooperative metaclass base. This enables the TestCase to be used in combination with other base classes that have custom metaclasses, such as mox.MoxTestBase. Only works with metaclasses that do not override type.__new__. Example: import google3 import mox from google.protobuf.internal import _parameterized class ExampleTest(parameterized.CoopTestCase(mox.MoxTestBase)): ... Args: other_base_class: (class) A test case base class. Returns: A new class object. CoopMetaclass CoopTestCase)re __metaclass__ror)other_base_classrs r rrsJ2% "$$) "B ( ((r )#rC __author__r=rerXrUuuidcollections.abcabcr ImportError collectionscompileruuid1hexr}objectr?rAr rrr$r'rNrbrirkrmreror[rrrFr r rsBoob1   (+++++++(((''''''( "*H I I TZ\\  VXX ))) 222$$$ 444>;>;>;>;>;V>;>;>;BEEE ***&: 8 8 8444"55555T5552>>>,GGGGGx ,BGGGG.(((((s ! --