forked from watson-developer-cloud/python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_examples.py
More file actions
44 lines (36 loc) · 1.45 KB
/
test_examples.py
File metadata and controls
44 lines (36 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# coding=utf-8
from __future__ import print_function
import re
import traceback
import pytest
import json as json_import
import os
from os.path import join, dirname
from glob import glob
# tests to exclude
excludes = ['authorization_v1.py', 'discovery_v1.ipynb', '__init__.py', 'microphone-speech-to-text.py']
# examples path. /examples
examples_path = join(dirname(__file__), '../', 'examples', '*.py')
@pytest.mark.skipif(os.getenv('VCAP_SERVICES') is None,
reason='requires VCAP_SERVICES')
def test_examples():
vcap_services = json_import.loads(os.getenv('VCAP_SERVICES'))
examples = glob(examples_path)
for example in examples:
name = example.split('/')[-1]
# exclude some tests cases like authorization
if name in excludes:
continue
# exclude tests if there are no credentials for that service
service_name = name[:-6] if not name.startswith('visual_recognition')\
else 'watson_vision_combined'
if service_name not in vcap_services:
print('%s does not have credentials in VCAP_SERVICES',
service_name)
continue
try:
service_file = open(example).read()
exec(re.sub(r'# coding[:=]\s*utf-8', '', service_file), globals())
except Exception as e:
assert False, 'example in file ' + name + ' failed with error: '\
+ str(e) + '\n' + traceback.format_exc()