Skip to content

Commit 5a13fe1

Browse files
authored
feat: tests for tns test command (#30)
Implement tests for `tns test` commands.
1 parent fefbe0b commit 5a13fe1

File tree

5 files changed

+163
-8
lines changed

5 files changed

+163
-8
lines changed

.pylintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ max-locals=15
528528
max-parents=7
529529

530530
# Maximum number of public methods for a class (see R0904).
531-
max-public-methods=20
531+
max-public-methods=25
532532

533533
# Maximum number of return / yield for function / method body.
534534
max-returns=6

products/nativescript/tns.py

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# pylint: disable=too-many-branches
22
import logging
33
import os
4-
54
import time
65

76
from core.base_test.test_context import TestContext
@@ -20,8 +19,8 @@ class Tns(object):
2019
@staticmethod
2120
def exec_command(command, cwd=Settings.TEST_RUN_HOME, platform=Platform.NONE, emulator=False, path=None,
2221
device=None, release=False, for_device=False, provision=Settings.IOS.DEV_PROVISION, bundle=False,
23-
hmr=False, aot=False, uglify=False, snapshot=False, log_trace=False, justlaunch=False, wait=True,
24-
timeout=600):
22+
hmr=False, aot=False, uglify=False, snapshot=False, log_trace=False, justlaunch=False,
23+
options=None, wait=True, timeout=600):
2524
"""
2625
Execute tns command.
2726
:param command: Tns command.
@@ -40,6 +39,7 @@ def exec_command(command, cwd=Settings.TEST_RUN_HOME, platform=Platform.NONE, em
4039
:param snapshot: If true pass `--env.snapshot` to command.
4140
:param log_trace: If not None pass `--log <level>` to command.
4241
:param justlaunch: If true pass `--justlaunch` to command.
42+
:param options: Pass additional options as string.
4343
:param wait: If true it will wait until command is complete.
4444
:param timeout: Timeout for CLI command (respected only if wait=True).
4545
:return: ProcessInfo object.
@@ -80,6 +80,8 @@ def exec_command(command, cwd=Settings.TEST_RUN_HOME, platform=Platform.NONE, em
8080
cmd += ' --justlaunch'
8181
if log_trace:
8282
cmd += ' --log trace'
83+
if options:
84+
cmd += ' ' + options
8385

8486
result = run(cmd=cmd, cwd=cwd, wait=wait, log_level=logging.INFO, timeout=timeout)
8587

@@ -294,6 +296,45 @@ def debug(app_name, platform, emulator=False, device=None, release=False, provis
294296
pass
295297
return result
296298

299+
@staticmethod
300+
def test_init(app_name, framework, verify=True):
301+
"""
302+
Execute `tns test init` command.
303+
:param app_name: App name (passed as --path <App name>)
304+
:param framework: Unit testing framework as string (jasmin, mocha, quinit).
305+
:param verify: Verify command was executed successfully.
306+
:return: Result of `tns test init` command.
307+
"""
308+
command = 'test init --framework {0}'.format(framework)
309+
result = Tns.exec_command(command=command, path=app_name, timeout=300)
310+
if verify:
311+
TnsAssert.test_initialized(app_name=app_name, framework=framework, output=result.output)
312+
return result
313+
314+
@staticmethod
315+
def test(app_name, platform, emulator=True, device=None, justlaunch=True, verify=True):
316+
"""
317+
Execute `tns test <platform>` command.
318+
:param app_name: App name (passed as --path <App name>)
319+
:param platform: PlatformType enum value.
320+
:param emulator: If true pass `--emulator` to the command.
321+
:param device: Pass `--device <value>` to command.
322+
:param justlaunch: If true pass `--justlaunch` to the command.
323+
:param verify: Verify command was executed successfully.
324+
:return: Result of `tns test` command.
325+
"""
326+
cmd = 'test {0}'.format(str(platform))
327+
result = Tns.exec_command(command=cmd, path=app_name, emulator=emulator, device=device, justlaunch=justlaunch)
328+
if verify:
329+
assert 'server started at' in result.output
330+
assert 'Launching browser' in result.output
331+
assert 'Starting browser' in result.output
332+
assert 'Connected on socket' in result.output
333+
assert 'Executed 1 of 1' in result.output
334+
assert 'TOTAL: 1 SUCCESS' in result.output \
335+
or 'Executed 1 of 1 SUCCESS' or 'Executed 1 of 1 SUCCESS' in result.output
336+
return result
337+
297338
@staticmethod
298339
def doctor(app_name=None):
299340
"""

products/nativescript/tns_assert.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,20 @@ def build(app_name, platform=None, release=False, provision=Settings.IOS.DEV_PRO
109109
# Assert size
110110
if app_data.size is not None:
111111
pass
112+
113+
# noinspection PyUnusedLocal
114+
@staticmethod
115+
def test_initialized(app_name, framework, output):
116+
# pylint: disable=unused-argument
117+
# TODO: Implement it!
118+
"""
119+
Execute `tns test init` command.
120+
:param app_name: App name (passed as --path <App name>)
121+
:param framework: Unit testing framework as string (jasmin, mocha, quinit).
122+
:param output: Output of `tns test init` command.
123+
:return: Result of `tns test init` command.
124+
"""
125+
if output is not None:
126+
assert 'Successfully installed plugin nativescript-unit-test-runner' in output
127+
assert 'Example test file created in' in output
128+
assert 'Run your tests using the' in output

tests/cli/test/test.md

Lines changed: 0 additions & 4 deletions
This file was deleted.

tests/cli/test/test_test.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# pylint: disable=unused-argument
2+
# pylint: disable=undefined-variable
3+
4+
import os
5+
6+
from parameterized import parameterized
7+
8+
from core.base_test.tns_test import TnsTest
9+
from core.enums.os_type import OSType
10+
from core.enums.platform_type import Platform
11+
from core.log.log import Log
12+
from core.settings import Settings
13+
from core.utils.device.device_manager import DeviceManager
14+
from core.utils.npm import Npm
15+
from data.templates import Template
16+
from products.nativescript.tns import Tns
17+
from products.nativescript.tns_assert import TnsAssert
18+
19+
APP_NAME = Settings.AppName.DEFAULT
20+
21+
TEST_DATA = [
22+
('jasmine-js-android', 'jasmine', Template.HELLO_WORLD_JS, Platform.ANDROID),
23+
('jasmine-ng-android', 'jasmine', Template.HELLO_WORLD_NG, Platform.ANDROID),
24+
('mocha-js-android', 'mocha', Template.HELLO_WORLD_JS, Platform.ANDROID),
25+
('mocha-ng-android', 'mocha', Template.HELLO_WORLD_NG, Platform.ANDROID),
26+
('qunit-js-android', 'qunit', Template.HELLO_WORLD_JS, Platform.ANDROID),
27+
]
28+
29+
TEST_DATA_OSX = [
30+
('jasmine-js-android', 'jasmine', Template.HELLO_WORLD_JS, Platform.ANDROID),
31+
('jasmine-js-ios', 'jasmine', Template.HELLO_WORLD_JS, Platform.IOS),
32+
('jasmine-ng-android', 'jasmine', Template.HELLO_WORLD_NG, Platform.ANDROID),
33+
('jasmine-ng-ios', 'jasmine', Template.HELLO_WORLD_NG, Platform.IOS),
34+
('mocha-js-android', 'mocha', Template.HELLO_WORLD_JS, Platform.ANDROID),
35+
('mocha-js-ios', 'mocha', Template.HELLO_WORLD_JS, Platform.IOS),
36+
('mocha-ng-android', 'mocha', Template.HELLO_WORLD_NG, Platform.ANDROID),
37+
('mocha-ng-ios', 'mocha', Template.HELLO_WORLD_NG, Platform.IOS),
38+
('qunit-js-android', 'qunit', Template.HELLO_WORLD_JS, Platform.ANDROID),
39+
('qunit-js-ios', 'qunit', Template.HELLO_WORLD_JS, Platform.IOS),
40+
]
41+
42+
43+
def get_data():
44+
if Settings.HOST_OS == OSType.OSX:
45+
return TEST_DATA_OSX
46+
else:
47+
return TEST_DATA
48+
49+
50+
# noinspection PyMethodMayBeStatic,PyUnusedLocal
51+
class PrepareAndBuildPerfTests(TnsTest):
52+
53+
@classmethod
54+
def setUpClass(cls):
55+
TnsTest.setUpClass()
56+
cls.emu = DeviceManager.Emulator.ensure_available(Settings.Emulators.DEFAULT)
57+
if Settings.HOST_OS is OSType.OSX:
58+
cls.sim = DeviceManager.Simulator.ensure_available(Settings.Simulators.DEFAULT)
59+
60+
def setUp(self):
61+
TnsTest.setUp(self)
62+
63+
@classmethod
64+
def tearDownClass(cls):
65+
TnsTest.tearDownClass()
66+
67+
@parameterized.expand(get_data())
68+
def test_100(self, title, framework, template, platform):
69+
# Create app
70+
Tns.create(app_name=APP_NAME, template=template.local_package)
71+
72+
# Add platforms
73+
if platform == Platform.ANDROID:
74+
Tns.platform_add_android(app_name=APP_NAME, framework_path=Settings.Android.FRAMEWORK_PATH)
75+
elif platform == Platform.IOS:
76+
Tns.platform_add_ios(app_name=APP_NAME, framework_path=Settings.IOS.FRAMEWORK_PATH)
77+
else:
78+
raise Exception('Unknown platform: ' + str(platform))
79+
80+
# Init tests and run tests
81+
if Settings.HOST_OS == OSType.WINDOWS and framework == 'qunit':
82+
# Hack for qunit on windows (see https://github.com/NativeScript/nativescript-cli/issues/4333)
83+
Npm.install(package='qunit@2', option='--save-dev', folder=os.path.join(Settings.TEST_RUN_HOME, APP_NAME))
84+
# Tns test init will still fail with exit code 1, so we use `verify=False` and then assert logs.
85+
result = Tns.test_init(app_name=APP_NAME, framework=framework, verify=False)
86+
TnsAssert.test_initialized(app_name=APP_NAME, framework=framework, output=result.output)
87+
else:
88+
Tns.test_init(app_name=APP_NAME, framework=framework)
89+
90+
# Run Tests
91+
if Settings.HOST_OS != OSType.WINDOWS:
92+
Tns.test(app_name=APP_NAME, platform=Platform.ANDROID, emulator=True, justlaunch=True)
93+
# TODO: Modify hello-world test with some real test (importing modules) and run the test again.
94+
else:
95+
Log.info('Due to unknown issues --justlauch do not exit on Windows when tests are executed on Jenkins!')
96+
# TODO: Fix it!
97+
98+
def test_400_invalid_framework_name(self):
99+
Tns.create(app_name=APP_NAME, template=Template.HELLO_WORLD_JS.local_package)
100+
result = Tns.test_init(app_name=APP_NAME, framework='jasmin', verify=False)
101+
assert 'Unknown or unsupported unit testing framework: jasmin' in result.output

0 commit comments

Comments
 (0)