Skip to content

Commit f8fb5de

Browse files
authored
Run integration tests in isolation (#302)
* add tmp folder fixture * exit at first failure since tests are inter-dependant * use fixtures to run tests in isolation * consistent use of the run function
1 parent 0c95087 commit f8fb5de

File tree

3 files changed

+63
-30
lines changed

3 files changed

+63
-30
lines changed

Diff for: test/conftest.py

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# This file is part of arduino-cli.
2+
3+
# Copyright 2019 ARDUINO SA (http://www.arduino.cc/)
4+
5+
# This software is released under the GNU General Public License version 3,
6+
# which covers the main part of arduino-cli.
7+
# The terms of this license can be found at:
8+
# https://www.gnu.org/licenses/gpl-3.0.en.html
9+
10+
# You can be released from the requirements of the above licenses by purchasing
11+
# a commercial license. Buying such a license is mandatory if you want to modify or
12+
# otherwise use the software for commercial activities involving the Arduino
13+
# software without disclosing the source code of your own applications. To purchase
14+
# a commercial license, send an email to [email protected].
15+
import os
16+
17+
import pytest
18+
from invoke import run
19+
20+
21+
@pytest.fixture(scope="session")
22+
def data_dir(tmpdir_factory):
23+
"""
24+
A tmp folder will be created before running
25+
the tests and deleted at the end.
26+
"""
27+
fn = tmpdir_factory.mktemp('ArduinoTest')
28+
return fn
29+
30+
31+
@pytest.fixture(scope="session")
32+
def run_command(data_dir):
33+
"""
34+
Provide a wrapper around invoke's `run` API so that every test
35+
will work in the same temporary folder.
36+
37+
Useful reference:
38+
http://docs.pyinvoke.org/en/1.2/api/runners.html#invoke.runners.Result
39+
"""
40+
cli_path = os.path.join(pytest.config.rootdir, '..', 'arduino-cli')
41+
env = {
42+
"ARDUINO_DATA_DIR": data_dir,
43+
"ARDUINO_DOWNLOADS_DIR": data_dir,
44+
"ARDUINO_SKETCHBOOK_DIR": data_dir
45+
}
46+
47+
def _run(cmd_string):
48+
cli_full_line = "{} {}".format(cli_path, cmd_string)
49+
return run(cli_full_line, echo=False, hide=True, warn=True, env=env)
50+
51+
return _run

Diff for: test/pytest.ini

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ filterwarnings =
77
markers =
88
slow: marks tests as slow (deselect with '-m "not slow"')
99

10-
addopts = -s --verbose --tb=short
10+
# atm some tests depend on each other, better to exit at first failure (-x)
11+
addopts = -x -s --verbose --tb=short

Diff for: test/test_main.py

+10-29
Original file line numberDiff line numberDiff line change
@@ -5,64 +5,45 @@
55
import semver
66
from datetime import datetime
77

8-
this_test_path = os.path.dirname(os.path.realpath(__file__))
9-
# Calculate absolute path of the CLI
10-
cli_path = os.path.join(this_test_path, '..', 'arduino-cli')
118

12-
# Useful reference:
13-
# http://docs.pyinvoke.org/en/1.2/api/runners.html#invoke.runners.Result
14-
15-
16-
def cli_line(*args):
17-
# Accept a list of arguments cli_line('lib list --format json')
18-
# Return a full command line string e.g. 'arduino-cli help --format json'
19-
cli_full_line = ' '.join([cli_path, ' '.join(str(arg) for arg in args)])
20-
return cli_full_line
21-
22-
23-
def run_command(*args):
24-
result = run(cli_line(*args), echo=False, hide=True, warn=True)
25-
return result
26-
27-
28-
def test_command_help():
9+
def test_command_help(run_command):
2910
result = run_command('help')
3011
assert result.ok
3112
assert result.stderr == ''
3213
assert 'Usage' in result.stdout
3314

3415

35-
def test_command_lib_list():
16+
def test_command_lib_list(run_command):
3617
"""
3718
When ouput is empty, nothing is printed out, no matter the output format
3819
"""
3920
result = run_command('lib list')
4021
assert result.ok
4122
assert '' == result.stderr
42-
result = run_command('lib list', '--format json')
23+
result = run_command('lib list --format json')
4324
assert '' == result.stdout
4425

4526

46-
def test_command_lib_install():
27+
def test_command_lib_install(run_command):
4728
libs = ['\"AzureIoTProtocol_MQTT\"', '\"CMMC MQTT Connector\"', '\"WiFiNINA\"']
4829
# Should be safe to run install multiple times
4930
result_1 = run_command('lib install {}'.format(' '.join(libs)))
5031
assert result_1.ok
5132
result_2 = run_command('lib install {}'.format(' '.join(libs)))
5233
assert result_2.ok
5334

54-
def test_command_lib_update_index():
35+
def test_command_lib_update_index(run_command):
5536
result = run_command('lib update-index')
5637
assert result.ok
5738
assert 'Updating index: library_index.json downloaded' == result.stdout.splitlines()[-1].strip()
5839

59-
def test_command_lib_remove():
40+
def test_command_lib_remove(run_command):
6041
libs = ['\"AzureIoTProtocol_MQTT\"', '\"CMMC MQTT Connector\"', '\"WiFiNINA\"']
6142
result = run_command('lib uninstall {}'.format(' '.join(libs)))
6243
assert result.ok
6344

6445
@pytest.mark.slow
65-
def test_command_lib_search():
46+
def test_command_lib_search(run_command):
6647
result = run_command('lib search')
6748
assert result.ok
6849
out_lines = result.stdout.splitlines()
@@ -81,7 +62,7 @@ def test_command_lib_search():
8162
assert number_of_libs == number_of_libs_from_json
8263

8364

84-
def test_command_board_list():
65+
def test_command_board_list(run_command):
8566
result = run_command('board list --format json')
8667
assert result.ok
8768
# check is a valid json and contains a list of ports
@@ -92,13 +73,13 @@ def test_command_board_list():
9273
assert 'protocol_label' in port
9374

9475

95-
def test_command_board_listall():
76+
def test_command_board_listall(run_command):
9677
result = run_command('board listall')
9778
assert result.ok
9879
assert ['Board', 'Name', 'FQBN'] == result.stdout.splitlines()[0].strip().split()
9980

10081

101-
def test_command_version():
82+
def test_command_version(run_command):
10283
result = run_command('version --format json')
10384
assert result.ok
10485
parsed_out = json.loads(result.stdout)

0 commit comments

Comments
 (0)