Skip to content

Added support for pytest in v2 for python-sdk#3

Merged
PranshuSrivastava merged 5 commits intokeploy:mainfrom
Hermione2408:HD-feat-621-pytest
Feb 15, 2024
Merged

Added support for pytest in v2 for python-sdk#3
PranshuSrivastava merged 5 commits intokeploy:mainfrom
Hermione2408:HD-feat-621-pytest

Conversation

@Hermione2408
Copy link
Contributor

@Hermione2408 Hermione2408 commented Dec 6, 2023

To run this test run
keploy test -c "python3 -m coverage run -m pytest test_keploy.py" --delay 10 --coverage
use this test file with flask-mongo application from samples-python repo

import pytest
import subprocess
import os
import requests
import logging
import sys
import time
sys.path.append('<path-to-python-sdk>")

from keployCLI import(
    fetch_test_sets,
    run_test_set,
    fetch_test_set_status,
    TestRunStatus,
    stop_user_application,
    start_user_application,
    find_coverage
    # cleanupProcesses
)

# Logger setup
logger = logging.getLogger('KeployCLI')
logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)

# Global variables
server_port = 16789
MAX_TIMEOUT = 100000 # 1 minute timeout for now

def test_keploy():
    testResult = True
    startTime = time.time()

    try:
        testSets = fetch_test_sets()
        if testSets is None:
            pytest.fail('Test sets are null')

        logger.info(f"TestSets: {testSets}")
        # Wait for keploy to start
        subprocess.call(["sleep", "10"])
        for testset in testSets:
            start_user_application("python3 app.py")
            subprocess.call(["sleep", "10"])
            result = True
            testRunId = run_test_set(testset)
            testRunStatus = None

            while True:
                # Check status every 2 sec
                subprocess.call(["sleep", "2"])
                testRunStatus = fetch_test_set_status(testRunId)
                if testRunStatus == TestRunStatus.RUNNING:
                    logger.info("testRun still in progress")

                    # Check if timeout has been reached
                    if time.time() - startTime > MAX_TIMEOUT:
                        logger.info("Timeout reached, exiting loop")
                        break

                    continue
                break

            if testRunStatus == TestRunStatus.FAILED:
                logger.info("testrun failed")
                result = False
            elif testRunStatus == TestRunStatus.PASSED:
                logger.info("testrun passed")
                result = True

            logger.info(f"TestResult of [{testset}]: {result}")
            testResult = testResult and result
            find_coverage(testset)
            stop_user_application()
            # wait 10 sec for server to stop
            subprocess.call(["sleep", "10"])
    # error
    except Exception as e:
        pytest.fail(f"Test execution failed: {str(e)}")

    assert testResult, "Test failed"

Hermione2408 and others added 4 commits February 15, 2024 14:46
Signed-off-by: Hermione Dadheech <hermionedadheech@gmail.com>
Signed-off-by: Pranshu Srivastava <iampranshu24@gmail.com>
Signed-off-by: Hermione Dadheech <hermionedadheech@gmail.com>
Signed-off-by: Pranshu Srivastava <iampranshu24@gmail.com>
Signed-off-by: Pranshu Srivastava <iampranshu24@gmail.com>
Signed-off-by: Pranshu Srivastava <iampranshu24@gmail.com>
Signed-off-by: Pranshu Srivastava <iampranshu24@gmail.com>
Copy link
Contributor

@PranshuSrivastava PranshuSrivastava left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@PranshuSrivastava PranshuSrivastava merged commit c2c50af into keploy:main Feb 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants