Skip to content
This repository was archived by the owner on Sep 14, 2023. It is now read-only.

Commit c03a545

Browse files
committed
Improved output on discovery and failures
* Avoids trace when tests fail and include clean error message * Improved how test are displayed when doing `pytest --only-collect`
1 parent 9a2a802 commit c03a545

File tree

1 file changed

+33
-15
lines changed

1 file changed

+33
-15
lines changed

pytest_molecule.py

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ def pytest_collect_file(parent, path):
3838

3939
class MoleculeFile(pytest.File):
4040
def collect(self):
41-
yield MoleculeItem("", self)
41+
yield MoleculeItem('test', self)
42+
43+
def __str__(self):
44+
return str(self.fspath.relto(os.getcwd()))
4245

4346

4447
class MoleculeItem(pytest.Item):
@@ -50,21 +53,36 @@ def runtest(self):
5053
cwd = os.path.abspath(os.path.join(self.fspath.dirname, '../..'))
5154
scenario = folders[-1]
5255
role = folders[-3] # noqa
53-
54-
cmd = ['python', '-m', 'molecule', 'test', '-s', scenario]
56+
cmd = [sys.executable, '-m', 'molecule', self.name, '-s', scenario]
5557
print("running: %s (from %s)" % (" " .join(cmd), cwd))
56-
# Workaround for STDOUT/STDERR line ordering issue:
57-
# https://github.com/pytest-dev/pytest/issues/5449
58-
p = subprocess.Popen(
59-
cmd,
60-
cwd=cwd,
61-
stdout=subprocess.PIPE,
62-
stderr=subprocess.STDOUT,
63-
universal_newlines=True)
64-
for line in p.stdout:
65-
print(line, end="")
66-
p.wait()
67-
assert p.returncode == 0
58+
59+
try:
60+
# Workaround for STDOUT/STDERR line ordering issue:
61+
# https://github.com/pytest-dev/pytest/issues/5449
62+
p = subprocess.Popen(
63+
cmd,
64+
cwd=cwd,
65+
stdout=subprocess.PIPE,
66+
stderr=subprocess.STDOUT,
67+
universal_newlines=True)
68+
for line in p.stdout:
69+
print(line, end="")
70+
p.wait()
71+
if p.returncode != 0:
72+
pytest.fail(
73+
"Error code %s returned by: %s" % (
74+
p.returncode, " ".join(cmd)),
75+
pytrace=False)
76+
except Exception as e:
77+
pytest.fail(
78+
"Exception %s returned by: %s" % (e, " ".join(cmd)),
79+
pytrace=False)
80+
81+
def reportinfo(self):
82+
return self.fspath, 0, "usecase: %s" % self.name
83+
84+
def __str__(self):
85+
return self.name
6886

6987

7088
class MoleculeException(Exception):

0 commit comments

Comments
 (0)