관리-도구
편집 파일: _parameterized.cpython-311.pyc
� �l[*'�"�� �x � d Z dZddlZddlZddlZddlZddlZ ddlmZ n# e $ r ddlZ Y nw xY w ej d� � Z ej � � j Z e� � Z e� � Zd� Zd� Zd� Zd� Z G d � d e� � Zd� Zd� Zd � Zd� Zd� Z G d� de� � Zd� Z G d� dej! e�� � Z!d� Z"dS )a� Adds 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]+\>c �R � t � dt | � � � � S )Nz<\1>)�ADDR_RE�sub�repr��objs ��/builddir/build/BUILD/imunify360-venv-2.5.2/opt/imunify360/venv/lib64/python3.11/site-packages/google/protobuf/internal/_parameterized.py� _CleanReprr � s � � ���W�d�3�i�i� (� (�(� c �$ � | j �d| j ��S )N�.)� __module__�__name__)�clss r � _StrClassr � s � ��N�N�N�C�L�L� 1�1r c �b � t | t j � � ot | t � � S �N)� isinstance�collections_abc�Iterable�strr s r �_NonStringIterabler � s, � � �S�/�2� 3� 3� #���c�"�"� "�$r c �& � t | t j � � r1d� d� | � � � D � � � � S t | � � r(d� t t | � � � � S t | f� � S )Nz, c 3 �D K � | ]\ }}|�d t |� � ��V � �dS )�=N)r )�.0�argname�values r � <genexpr>z'_FormatParameterList.<locals>.<genexpr>� sV � � � � D� D�'��%� !(����E�):�):�):�;� D� D� D� D� D� Dr ) r r �Mapping�join�itemsr �mapr �_FormatParameterList)�testcase_paramss r r$ r$ � s� � ����!8�9�9� 4��9�9� D� D�+:�+@�+@�+B�+B�D� D� D� D� D� D��/�*�*� 4��9�9�S��_�5�5�6�6�6��� 2�3�3�3r c �$ � e Zd ZdZd� Zd� Zd� ZdS )�_ParameterizedTestIterz9Callable and iterable class for producing new test cases.c �0 � || _ || _ || _ 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! � � $�D���D�N�#�D���r c � � t d� � �)Nz�You 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, �args�kwargss r �__call__z_ParameterizedTestIter.__call__� s � � � A� B� B� Br c �V ���� | j �| j ���fd���fd�| j D � � S )Nc � �� � t j �� � �� fd�� � }�t u r5d|_ |xj t � d � � z c_ � dd � � n4�t u rdt � � � �d�|_ nt ��d�� � �|j �dt � � � �d�|_ �j r|xj d�j ��z c_ |S ) Nc � �� t �t j � � r �| fi ��� d S t �� � r �| g��R � d S �| �� � d S r )r r r r )r, r- r% s ��r �BoundParamTestzS_ParameterizedTestIter.__iter__.<locals>.MakeBoundParamTest.<locals>.BoundParamTest� s| �� ��o��'>�?�?� -� �+�d� .� .�o� .� .� .� .� .� �� 0� 0� -� �+�d� -�_� -� -� -� -� -� -� �+�d�O� ,� ,� ,� ,� ,r Tr � �(�)z is not a valid naming type.� )� functools�wraps� _FIRST_ARG�__x_use_name__r r �_ARGUMENT_REPRr$ �__x_extra_id__r1 �__doc__)r% r8 r. r- s ` ��r �MakeBoundParamTestz;_ParameterizedTestIter.__iter__.<locals>.MakeBoundParamTest� s ��� ���{�#�#�-� -� -� -� -� $�#�-� � � "� "� )-��%����3��q�'9�#:�#:�:���)�!�"�"�-����.�(�(�(� !��1�1�1�1�)4��%�%� �{�{�{�L�M�M�M� � !� !� !�#7��#H�#H�#H�#H� J�n�� � � B�����K�,?�,?�"A�A��� �r c 3 �. �K � | ]} �|� � V � �d S r � )r �crD s �r r z2_ParameterizedTestIter.__iter__.<locals>.<genexpr>� s/ �� � � �:�:�a���q�!�!�:�:�:�:�:�:r )r) r+ r* )r, rD r. r- s @@@r �__iter__z_ParameterizedTestIter.__iter__� sQ ���� ��#�K��#�K�� � � � � �>