Dataiku API tests

Solved!
afostor
Level 2
Dataiku API tests

Hi,

I would like to know if it is possible to write unit tests to a Dataiku API code (in Python) using the API Designer, in order to check if it works according to the desired functionality before the API is deployed.

Thanks


Operating system used: Windows

0 Kudos
1 Solution
AlexT
Dataiker

Hi @afostor ,
There is no specific way to do this in the API endpoint. Usually testing is done using the test queries in API Designer directly.

If you want to perform additional test, you can write a unit test directly in the API endpoint code directly and only invoke this when using param perform_test= true.

import unittest
import traceback

# Insert here initialization code
class TestApiPyFunction(unittest.TestCase):

    def test_api_py_function(self):
        # Test case 1 pass
        result = api_py_function(2, 3, 4)
        self.assertEqual(result, 2 + 3 * 4)  # Expected: 2 + 3 * 4 = 14

        # Test case 2 (Intentional failure)
        result = api_py_function(0, 5, 10)
        self.assertEqual(result, 0 + 5 * 10 + 1)  # Expected: 0 + 5 * 10 + 1 = 51

 
        return "All tests passed successfully."

def api_py_function(param1, param2, param3, perform_test=False):
    result = param1 + param2 * param3
    if perform_test:
        try:
            # Run the test cases if perform_test is True
            # The test cases are defined below in the TestApiPyFunction class
            test_result = TestApiPyFunction().test_api_py_function()
        except AssertionError as e:
            tb = traceback.format_exc()
            return result, tb
        return result, "All tests passed successfully."
    return result

 

View solution in original post

2 Replies
AlexT
Dataiker

Hi @afostor ,
There is no specific way to do this in the API endpoint. Usually testing is done using the test queries in API Designer directly.

If you want to perform additional test, you can write a unit test directly in the API endpoint code directly and only invoke this when using param perform_test= true.

import unittest
import traceback

# Insert here initialization code
class TestApiPyFunction(unittest.TestCase):

    def test_api_py_function(self):
        # Test case 1 pass
        result = api_py_function(2, 3, 4)
        self.assertEqual(result, 2 + 3 * 4)  # Expected: 2 + 3 * 4 = 14

        # Test case 2 (Intentional failure)
        result = api_py_function(0, 5, 10)
        self.assertEqual(result, 0 + 5 * 10 + 1)  # Expected: 0 + 5 * 10 + 1 = 51

 
        return "All tests passed successfully."

def api_py_function(param1, param2, param3, perform_test=False):
    result = param1 + param2 * param3
    if perform_test:
        try:
            # Run the test cases if perform_test is True
            # The test cases are defined below in the TestApiPyFunction class
            test_result = TestApiPyFunction().test_api_py_function()
        except AssertionError as e:
            tb = traceback.format_exc()
            return result, tb
        return result, "All tests passed successfully."
    return result

 

afostor
Level 2
Author

Thanks, I will try this way.

0 Kudos