1

I have rootFile = root.json file which content is

{
  "tests":[
    {
      "test":"test1",
      "url":"url1"
    },
    {
      "test":"test2",
      "url":"url2"
    },
    {
      "test":"test3",
      "url":"url3"
    }
  ]
}

and I have python function to which I am giving string params to run

def check(params):
    runId=time.strftime("%Y%m%d-%H%M%S")
        outputFile=Path(""+runId+".txt")
    with open (rootFile) as rj:
        data=json.load(rj)
    for param in params:
        for t in data['tests']:
            if t['test'] == param:
                urlToUse=t['url']
                testrun(param, urlToUse, runId)
            else:
                nonExistingTest="Test "+param+" Doesn't exist \n"
                if not outputFile.exists():
                    with open(outputFile,"a") as noSuchTest:
                        noSuchTest.write("Test "+param+" Doesn't exist \n")
                elif not nonExistingTest in open(outputFile).read():
                    with open(outputFile,"a") as noSuchTest:
                        noSuchTest.write("Test "+param+" Doesn't exist \n")
    with open(outputFile,"r") as pf:
        message=pf.read()
        slackResponse(message)

when my params is "test1 test2 test3" which exist in root.json file I am getting such response

Test test1 passed #this response comes from testrun() function
Test test1 Doesn't exist

Test test2 Doesn't exist
Test test2 passed  #this response comes from testrun() function

Test test3 Doesn't exist
Test test3 passed  #this response comes from testrun() function

But when I am giving non existing params the output is correct. e.g

Test test4 Doesn't exist
Test test5 Doesn't exist
Test test6 Doesn't exist
Test test7 Doesn't exist

Can't understand why it's sending doesn't exist when it's actually exists

1 Answer 1

1

You are comparing each parameter passed with the function call to every item of the tests array loaded from the json file, starting the corresponding test on equivalence, and echoing a message saying that such a test does not exist otherwise. Because this comparison will only have a positive result once per parameter, but will be checked as many times as there are tests specified in root.json for each parameter, there will be a lot of lines in the output indicating that a particular parameter doesn't match a particular test specified in root.json.

You'll need some way to leave the loop once you've found the root.json entry that is being adressed by the current parameter. I'd recommend either changing the data structure assigned to tests in root.json from an array to an object, with the test names as keys and their urls as values, or somehow filter the list of possible tests that you compare the current parameter with.

Consider changing everything inside for param in params: into the following:

matching_tests = [t for t in data['tests'] if t['test'] == param]
if len(matching_tests) > 0:
    for t in matching_tests:
        urlToUse=t['url']
        testrun(param, urlToUse, runId)
else:
    nonExistingTest="Test "+param+" Doesn't exist \n"
    [...]

That way, the message saying that there is not test matching a given parameter will only be echoed at most once.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.