Skip to content

Commit 7476d6f

Browse files
s0undt3chdwoz
authored andcommitted
Don't group fixtures, and improve them
Signed-off-by: Pedro Algarvio <palgarvio@vmware.com>
1 parent 38fb76c commit 7476d6f

11 files changed

+656
-524
lines changed

tests/pytests/functional/modules/test_mac_assistive.py

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,40 +18,39 @@ def assistive(modules):
1818
return modules.assistive
1919

2020

21-
@pytest.fixture(scope="function")
22-
def osa_script():
23-
yield "/usr/bin/osascript"
24-
25-
26-
@pytest.fixture(scope="function", autouse=True)
27-
def _setup_teardown_vars(assistive, osa_script):
21+
@pytest.fixture
22+
def osa_script(assistive):
23+
osa_script_path = "/usr/bin/osascript"
2824
try:
29-
ret = assistive.install(osa_script, True)
25+
ret = assistive.install(osa_script_path, True)
26+
yield osa_script_path
3027
except CommandExecutionError as exc:
31-
pytest.skip(f"Unable to install {osa_script} - {str(exc.value)}")
32-
33-
try:
34-
yield
28+
pytest.skip(f"Unable to install {osa_script}: {exc}")
3529
finally:
36-
osa_script_ret = assistive.installed(osa_script)
30+
osa_script_ret = assistive.installed(osa_script_path)
3731
if osa_script_ret:
38-
assistive.remove(osa_script)
32+
assistive.remove(osa_script_path)
3933

40-
smile_bundle = "com.smileonmymac.textexpander"
34+
35+
@pytest.fixture
36+
def install_remove_pkg_name(assistive):
37+
smile_bundle = "com.smileonmymac.textexpander"
38+
try:
39+
yield smile_bundle
40+
finally:
4141
smile_bundle_present = assistive.installed(smile_bundle)
4242
if smile_bundle_present:
4343
assistive.remove(smile_bundle)
4444

4545

4646
@pytest.mark.slow_test
47-
def test_install_and_remove(assistive, osa_script):
47+
def test_install_and_remove(assistive, install_remove_pkg_name):
4848
"""
4949
Tests installing and removing a bundled ID or command to use assistive access.
5050
"""
51-
new_bundle = "com.smileonmymac.textexpander"
52-
ret = assistive.install(new_bundle)
51+
ret = assistive.install(install_remove_pkg_name)
5352
assert ret
54-
ret = assistive.remove(new_bundle)
53+
ret = assistive.remove(install_remove_pkg_name)
5554
assert ret
5655

5756

tests/pytests/functional/modules/test_mac_brew_pkg.py

Lines changed: 48 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -19,96 +19,82 @@ def pkg(modules):
1919
return modules.pkg
2020

2121

22-
# Brew doesn't support local package installation - So, let's
23-
# Grab some small packages available online for brew
24-
25-
26-
@pytest.fixture(scope="function")
27-
def add_pkg():
28-
yield "algol68g"
29-
22+
@pytest.fixture
23+
def pkg_1_name(pkg):
24+
pkg_name = "algol68g"
25+
try:
26+
yield pkg_name
27+
finally:
28+
pkg_list = pkg.list_pkgs()
3029

31-
@pytest.fixture(scope="function")
32-
def del_pkg():
33-
yield "acme"
30+
# Remove package if installed
31+
if pkg_name in pkg_list:
32+
pkg.remove(pkg_name)
3433

3534

36-
@pytest.fixture(scope="function", autouse=True)
37-
def _setup_teardown_vars(pkg, add_pkg, del_pkg):
35+
@pytest.fixture
36+
def pkg_2_name(pkg):
37+
pkg_name = "acme"
3838
try:
39-
yield
39+
pkg.install(pkg_name)
40+
pkg_list = pkg.list_pkgs()
41+
if pkg_name not in pkg_list:
42+
pytest.skip(f"Failed to install the '{pkg_name}' package to delete")
43+
yield pkg_name
4044
finally:
4145
pkg_list = pkg.list_pkgs()
4246

43-
# Remove any installed packages
44-
if add_pkg in pkg_list:
45-
pkg.remove(add_pkg)
46-
if del_pkg in pkg_list:
47-
pkg.remove(del_pkg)
47+
# Remove package if still installed
48+
if pkg_name in pkg_list:
49+
pkg.remove(pkg_name)
4850

4951

50-
def test_brew_install(pkg, add_pkg):
52+
def test_brew_install(pkg, pkg_1_name):
5153
"""
5254
Tests the installation of packages
5355
"""
54-
pkg.install(add_pkg)
56+
pkg.install(pkg_1_name)
5557
pkg_list = pkg.list_pkgs()
56-
assert add_pkg in pkg_list
58+
assert pkg_1_name in pkg_list
5759

5860

59-
def test_remove(pkg, del_pkg):
61+
def test_remove(pkg, pkg_2_name):
6062
"""
6163
Tests the removal of packages
6264
"""
63-
# Install a package to delete - If unsuccessful, skip the test
64-
pkg.install(del_pkg)
65+
pkg.remove(pkg_2_name)
6566
pkg_list = pkg.list_pkgs()
66-
if del_pkg not in pkg_list:
67-
pkg.install(del_pkg)
68-
pytest.skip("Failed to install a package to delete")
69-
70-
# Now remove the installed package
71-
pkg.remove(del_pkg)
72-
del_list = pkg.list_pkgs()
73-
assert del_pkg not in del_list
67+
assert pkg_2_name not in pkg_list
7468

7569

76-
def test_version(pkg, add_pkg):
70+
def test_version(pkg, pkg_1_name):
7771
"""
7872
Test pkg.version for mac. Installs a package and then checks we can get
7973
a version for the installed package.
8074
"""
81-
pkg.install(add_pkg)
75+
pkg.install(pkg_1_name)
8276
pkg_list = pkg.list_pkgs()
83-
version = pkg.version(add_pkg)
84-
assert version, f"version: {version} is empty, or other issue is present"
85-
assert (
86-
add_pkg in pkg_list
87-
), "package: {} is not in the list of installed packages: {}".format(
88-
add_pkg, pkg_list
89-
)
77+
version = pkg.version(pkg_1_name)
78+
assert version
79+
assert pkg_1_name in pkg_list
9080
# make sure the version is accurate and is listed in the pkg_list
91-
assert version in str(
92-
pkg_list[add_pkg]
93-
), "The {} version: {} is not listed in the pkg_list: {}".format(
94-
add_pkg, version, pkg_list[add_pkg]
95-
)
81+
assert version in str(pkg_list[pkg_1_name])
9682

9783

98-
def test_latest_version(pkg, add_pkg):
84+
def test_latest_version(pkg, pkg_1_name):
9985
"""
10086
Test pkg.latest_version:
10187
- get the latest version available
10288
- install the package
10389
- get the latest version available
10490
- check that the latest version is empty after installing it
10591
"""
106-
pkg.remove(add_pkg)
107-
uninstalled_latest = pkg.latest_version(add_pkg)
92+
pkg.remove(pkg_1_name)
93+
uninstalled_latest = pkg.latest_version(pkg_1_name)
10894

109-
pkg.install(add_pkg)
110-
installed_latest = pkg.latest_version(add_pkg)
111-
version = pkg.version(add_pkg)
95+
pkg.install(pkg_1_name)
96+
installed_latest = pkg.latest_version(pkg_1_name)
97+
version = pkg.version(pkg_1_name)
11298
assert isinstance(uninstalled_latest, str)
11399
assert installed_latest == version
114100

@@ -121,10 +107,9 @@ def test_refresh_db(pkg):
121107
assert refresh_brew
122108

123109

124-
def test_list_upgrades(pkg, add_pkg):
110+
def test_list_upgrades(pkg, pkg_1_name):
125111
"""
126-
Test pkg.list_upgrades: data is in the form {'name1': 'version1',
127-
'name2': 'version2', ... }
112+
Test pkg.list_upgrades: data is in the form {'name1': 'version1', 'name2': 'version2', ... }
128113
"""
129114
upgrades = pkg.list_upgrades()
130115
assert isinstance(upgrades, dict)
@@ -134,14 +119,14 @@ def test_list_upgrades(pkg, add_pkg):
134119
assert isinstance(upgrades[name], str)
135120

136121

137-
def test_info_installed(pkg, add_pkg):
122+
def test_info_installed(pkg, pkg_1_name):
138123
"""
139124
Test pkg.info_installed: info returned has certain fields used by
140125
mac_brew.latest_version
141126
"""
142-
pkg.install(add_pkg)
143-
info = pkg.info_installed(add_pkg)
144-
assert add_pkg in info
145-
assert "versions" in info[add_pkg]
146-
assert "revision" in info[add_pkg]
147-
assert "stable" in info[add_pkg]["versions"]
127+
pkg.install(pkg_1_name)
128+
info = pkg.info_installed(pkg_1_name)
129+
assert pkg_1_name in info
130+
assert "versions" in info[pkg_1_name]
131+
assert "revision" in info[pkg_1_name]
132+
assert "stable" in info[pkg_1_name]["versions"]

0 commit comments

Comments
 (0)