Menu

US Region

Grandmetric LLC
Lewes DE 19958
16192 Coastal Hwy USA
EIN: 98-1615498
+1 302 691 94 10
info@grandmetric.com

EMEA Region

GRANDMETRIC Sp. z o.o.
ul. Metalowa 5, 60-118 Poznań, Poland
NIP 7792433527
+48 61 271 04 43
info@grandmetric.com

UK

Grandmetric LTD
Office 584b
182-184 High Street North
London
E6 2JA
+44 20 3321 5276
info@grandmetric.com

  • en
  • pl
  • DDT library for Python

    Decorators and examples

    DDT library for Python – examples

    Date: 27.07.2021



    Automated testing without the usage of the DDT library is like a cake without the icing – a bit dry. Data-Driven Tests are the most helpful when you want to run test cases with multiple values of variables. Instead of looping through inside a test case, you can use decorators (now the cake parallel doesn’t seem so dumb, does it?) to make that for you.

    DDT decorators

    DDT library for Python is a short and sweet tool that consists of four decorators:

    • @ddt, which is a class decorator
    • @data or @file_data, which are method decorators
    • @unpack which helps if you work with tuples or lists passed as variables

    You only have to use @unpack if you want to pass multiple variables to the test.

    Example

    @data([“Anna”, “Bayer”], [“Mark”, “Darcy”], [None, “Jones”])
    @unpack
    def test_should_validate_names_and_surnames(self, name, surname):
        self.assertIsNotNone(name
        self.assertIsNotNone(surname)

    As you can see, we have two variables: name and surname and we pass different values for those variables. The test first takes Anna as a name and Bayer as a surname and checks if either name or surname is a None value. If it’s other than None – the test passes. So you already know that the third iteration of this test won’t pass, as Mr or Mrs. Jones doesn’t have a name.

    If you want to pass multiple variations of one variable, you can simply use:

    @data(1, 2, 4)
    def test_should_check_id_existence(self, id):
        ids_from_database = get_ids()
        self.assertIn(id, Id_from_database)

    This example calls an external function – get_ids() which returns a list of IDs that are in our imaginary database. Next, an assertion occurs – we check if ID given by the @data decorator is present in our database. If it is – the test passes. If it’s not – the test fails.

    You can also use data from files, hence the @file_data decorator. The files must be either of YAML or JSON type. Keep in mind that the default file type is JSON.

    Usage of @file_data decorator is shown below:

    @file_data('data.json')   
    def test_should_verify_uuidt(self, uuid):    
        proper_length = 19   
        self.assertEqual(proper_length, uuid.len())

    Remember that if you are using PyCharm and use its built-in tools like this green triangle next to your test code, it might not find tests when using DDT library. This might cause errors like this one:

    DDT library showing a failed test

    The easiest solution is to click the “play button” located next to your class name. That way all of the tests in that class are visible to our interpreter.

    DDT library – key takeaways

    To sum everything up – DDT is a handy tool to make test automation easier. It is useful when you have to test functionality that has many variables and they all have to be checked before accepting a task. But don’t go overboard and make multiple tests every time you test something.

    It is not necessary to try 50 different UUIDs if they have the right UUID format. Or to check if 60 different names written in the same alphabet are parsing right. Be wise when picking your testing data to cover as many cases as possible but at the same time use a variety of data type.

    Looking for other Python resources to learn about? Check our post on aiohttp library routing order.

    Author

    Emilia Nowacka

    Emilia is a Python programmer specializing in software testing. Her academic work focuses on machine learning and neural networks. In her free time, she trains for half-marathons and enjoys reading.

    Leave a Reply

    Your email address will not be published. Required fields are marked *


    Grandmetric