Skip to content

Commit d5b8a1e

Browse files
authored
Improve pytest, adding pytest.ini and set default arguments (appium#284)
1 parent 864106c commit d5b8a1e

5 files changed

Lines changed: 112 additions & 57 deletions

File tree

ci-requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ isort==4.3.4
44
pylint==1.9.3
55
autopep8==1.4.3
66
httpretty==0.9.6
7+
pytest-cov==2.6.0

test/pytest.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[pytest]
2+
addopts = -ra -q --cov=appium

test/unit/helper/test_helper.py

Lines changed: 103 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -12,66 +12,118 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import unittest
16-
import httpretty
1715
import json
16+
import httpretty
1817

1918
from appium import webdriver
2019

20+
# :return: A string of test URL
21+
SERVER_URL_BASE = 'http://localhost:4723/wd/hub'
22+
23+
24+
def appium_command(command):
25+
"""
26+
Return a command of Appium
27+
28+
:return: A string of command URL
29+
"rtype: string
30+
"""
31+
return '{}{}'.format(SERVER_URL_BASE, command)
32+
2133

22-
class TestHelper():
23-
24-
@staticmethod
25-
def mock_android_driver():
26-
"""
27-
Return a driver which is generated a mock response
28-
29-
:return: An instance of WebDriver
30-
:rtype: WebDriver
31-
"""
32-
33-
response_body_json = json.dumps(
34-
{
35-
'value': {
36-
'sessionId': '1234567890',
37-
'capabilities': {
38-
'platform': 'LINUX',
39-
'desired': {
40-
'platformName': 'Android',
41-
'automationName': 'uiautomator2',
42-
'platformVersion': '7.1.1',
43-
'deviceName': 'Android Emulator',
44-
'app': '/test/apps/ApiDemos-debug.apk',
45-
},
34+
def android_w3c_driver():
35+
"""
36+
Return a W3C driver which is generated by a mock response for Android
37+
38+
:return: An instance of WebDriver
39+
:rtype: WebDriver
40+
"""
41+
42+
response_body_json = json.dumps(
43+
{
44+
'value': {
45+
'sessionId': '1234567890',
46+
'capabilities': {
47+
'platform': 'LINUX',
48+
'desired': {
4649
'platformName': 'Android',
4750
'automationName': 'uiautomator2',
4851
'platformVersion': '7.1.1',
49-
'deviceName': 'emulator-5554',
52+
'deviceName': 'Android Emulator',
5053
'app': '/test/apps/ApiDemos-debug.apk',
51-
'deviceUDID': 'emulator-5554',
52-
'appPackage': 'com.example.android.apis',
53-
'appWaitPackage': 'com.example.android.apis',
54-
'appActivity': 'com.example.android.apis.ApiDemos',
55-
'appWaitActivity': 'com.example.android.apis.ApiDemos'
56-
}
54+
},
55+
'platformName': 'Android',
56+
'automationName': 'uiautomator2',
57+
'platformVersion': '7.1.1',
58+
'deviceName': 'emulator-5554',
59+
'app': '/test/apps/ApiDemos-debug.apk',
60+
'deviceUDID': 'emulator-5554',
61+
'appPackage': 'com.example.android.apis',
62+
'appWaitPackage': 'com.example.android.apis',
63+
'appActivity': 'com.example.android.apis.ApiDemos',
64+
'appWaitActivity': 'com.example.android.apis.ApiDemos'
5765
}
5866
}
59-
)
60-
61-
httpretty.register_uri(
62-
httpretty.POST,
63-
'http://localhost:4723/wd/hub/session',
64-
body=response_body_json
65-
)
66-
67-
desired_caps = {
68-
'platformName': 'Android',
69-
'deviceName': 'Android Emulator',
70-
'app': 'path/to/app',
71-
'automationName': 'UIAutomator2'
7267
}
73-
driver = webdriver.Remote(
74-
'http://localhost:4723/wd/hub',
75-
desired_caps
76-
)
77-
return driver
68+
)
69+
70+
httpretty.register_uri(
71+
httpretty.POST,
72+
appium_command('/session'),
73+
body=response_body_json
74+
)
75+
76+
desired_caps = {
77+
'platformName': 'Android',
78+
'deviceName': 'Android Emulator',
79+
'app': 'path/to/app',
80+
'automationName': 'UIAutomator2'
81+
}
82+
83+
driver = webdriver.Remote(
84+
SERVER_URL_BASE,
85+
desired_caps
86+
)
87+
return driver
88+
89+
90+
def ios_w3c_driver():
91+
"""
92+
Return a W3C driver which is generated by a mock response for iOS
93+
94+
:return: An instance of WebDriver
95+
:rtype: WebDriver
96+
"""
97+
98+
response_body_json = json.dumps(
99+
{
100+
'value': {
101+
'sessionId': '1234567890',
102+
'capabilities': {
103+
'device': 'iphone',
104+
'browserName': 'UICatalog',
105+
'sdkVersion': '11.4',
106+
'CFBundleIdentifier': 'com.example.apple-samplecode.UICatalog'
107+
}
108+
}
109+
}
110+
)
111+
112+
httpretty.register_uri(
113+
httpretty.POST,
114+
appium_command('/session'),
115+
body=response_body_json
116+
)
117+
118+
desired_caps = {
119+
'platformName': 'iOS',
120+
'deviceName': 'iPhone Simulator',
121+
'app': 'path/to/app',
122+
'automationName': 'XCUITest'
123+
}
124+
125+
driver = webdriver.Remote(
126+
SERVER_URL_BASE,
127+
desired_caps
128+
)
129+
return driver

test/unit/webdriver/device/clipboard_test.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from test.unit.helper.test_helper import TestHelper
15+
from test.unit.helper.test_helper import appium_command, android_w3c_driver, ios_w3c_driver
1616

1717
import json
1818
import httpretty
@@ -25,10 +25,10 @@ class TestWebDriverDeviceClipboard(object):
2525

2626
@httpretty.activate
2727
def test_set_clipboard_with_url(self):
28-
driver = TestHelper.mock_android_driver()
28+
driver = android_w3c_driver()
2929
httpretty.register_uri(
3030
httpretty.POST,
31-
'http://localhost:4723/wd/hub/session/1234567890/appium/device/set_clipboard',
31+
appium_command('/session/1234567890/appium/device/set_clipboard'),
3232
body='{"value": ""}'
3333
)
3434
driver.set_clipboard(appium_bytes(str('http://appium.io/'), 'UTF-8'),
@@ -41,10 +41,10 @@ def test_set_clipboard_with_url(self):
4141

4242
@httpretty.activate
4343
def test_set_clipboard_text(self):
44-
driver = TestHelper.mock_android_driver()
44+
driver = ios_w3c_driver()
4545
httpretty.register_uri(
4646
httpretty.POST,
47-
'http://localhost:4723/wd/hub/session/1234567890/appium/device/set_clipboard',
47+
appium_command('/session/1234567890/appium/device/set_clipboard'),
4848
body='{"value": ""}'
4949
)
5050
driver.set_clipboard_text('hello')

test/unit/webdriver/webdriver_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import json
1616
import httpretty
17-
17+
from test.unit.helper.test_helper import android_w3c_driver, ios_w3c_driver
1818
from appium import webdriver
1919

2020

0 commit comments

Comments
 (0)