I'm trying to send my Behave test results to an API Endpoint. I set the output file to be a new JSON file, run my test, and then in the Behave after_all()
send the JSON result via the requests
package.
I'm running my Behave test like so:
args = ['--outfile=/home/user/nathan/results/behave4.json',
'--for mat=json.pretty']
from behave.__main__ import main as behave_main
behave_main(args)
In my environment.py
's after_all()
, I have:
def after_all(context):
data = json.load(open('/home/user/myself/results/behave.json', 'r')) # This line causes the error
sendoff = {}
sendoff['results'] = data
r = requests.post(MyAPIEndpoint, json=sendoff)
I'm getting the following error when running my Behave test:
HOOK-ERROR in after_all: ValueError: Expecting object: line 124 column 1
(char 3796)
ABORTED: By user.
The reported error is here in my JSON file:
[
{
...
} <-- line 124, column 1
]
However, behave.json
is outputted after the run and according to JSONLint it is valid JSON. I don't know the exact details of after_all()
, but I think the issue is that the JSON file isn't done writing by the time I try to open it in after_all()
. If I try json.load()
a second time on the behave.json
file after the file is written, it runs without error and I am able to view my JSON file at the endpoint.
Any better explanation as to why this is happening? Any solution or change in logic to get past this?
Yes, it seems as though the file is still in the process of being written when I try to access it in after_all()
. I put in a small delay before I open the file in my code, then I manually viewed the behave.json
file and saw that there was no closing ]
after the last }
.
That explains that. I will create a new question to find out how to get by this, or if a change in a logic is required.