Skip to content

Commit

Permalink
Merge pull request #605 from nose-devs/add-fix-for-subproc-tests
Browse files Browse the repository at this point in the history
Add a fix for nose2's own subprocess tests (on top of #601)
  • Loading branch information
sirosen committed Apr 27, 2024
2 parents 0610f47 + d838581 commit 3de648a
Show file tree
Hide file tree
Showing 15 changed files with 81 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*$py.class
*.orig
example.cfg
.coverage
.coverage*
.idea
nosetests.xml
xunit.xml
Expand Down
5 changes: 3 additions & 2 deletions nose2/plugins/coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def __init__(self):
self.covConfig = (
self.config.as_str("coverage-config", "").strip() or ".coveragerc"
)
self.covCombine = self.config.as_bool("coverage-combine", False)

group = self.session.pluginargs
group.add_argument(
Expand Down Expand Up @@ -125,8 +126,8 @@ def beforeSummaryReport(self, event):
# requesting a better fix in nedbat/coveragepy#34
self.covController.save()

if self._mpmode:
self.covController.combine(strict=True)
if self.covCombine or self._mpmode:
self.covController.combine(strict=self._mpmode)

percent_coverage = None

Expand Down
13 changes: 13 additions & 0 deletions nose2/tests/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,19 @@ def assertTestRunOutputMatches(self, proc, stdout=None, stderr=None):
if stderr:
self.assertRegex(util.safe_decode(cmd_stderr), stderr)

def runInProc(self, working_dir, *args, timeout=10):
if not os.path.isabs(working_dir):
working_dir = support_file(working_dir)

proc = subprocess.Popen(
[sys.executable, "-m", "nose2"] + list(args),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=working_dir,
)
proc.wait(timeout=timeout)
return proc

def runIn(self, testdir, *args, **kw):
return run_nose2(*args, cwd=testdir, **kw)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[run]
concurrency = multiprocessing
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from multiprocessing import Process


def method2():
return


def method():
p = Process(target=method2)
p.start()
p.join()
return True
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[coverage]
always-on = True
coverage = lib
coverage-config = .coveragerc
coverage-combine = True
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import unittest

from lib.mod1 import method


class TestLib(unittest.TestCase):
def test1(self):
self.assertEqual(method(), True)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[run]
concurrency = multiprocessing
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from multiprocessing import Process


def method2():
return


def method():
p = Process(target=method2)
p.start()
p.join()
return True
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[coverage]
always-on = True
coverage = lib
coverage-config = .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import unittest

from lib.mod1 import method


class TestLib(unittest.TestCase):
def test1(self):
self.assertEqual(method(), True)
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
import time

# statement run at import time should be covered
foo = 1

with open("/tmp/showme", "a") as f:
f.write("\n\nbeta" + str(time.time()))


raise SystemExit


# so should an ordinary function body
def func():
Expand Down
14 changes: 11 additions & 3 deletions nose2/tests/functional/test_coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,8 @@ def test_run_with_mp(self):
total_stats=r"\s+8\s+5\s+38%",
)

# FIXME: figure out why this fails and remove @skip
@unittest.skip("fails in testsuite but passes in real-world conditions")
def test_measures_imports(self):
proc = self.runIn(
proc = self.runInProc(
"scenario/coverage_of_imports",
"-v",
"--with-coverage",
Expand Down Expand Up @@ -136,3 +134,13 @@ def test_run_coverage_fail_under2(self):
total_stats=TOTAL_STATS,
assert_exit_status=1,
)

def test_run_coverage_with_combine(self):
"""Check coverage with combine"""
proc = self.runInProc("scenario/coverage_multiprocessing_with_combine", "-v")
self.assertProcOutputPattern(proc, "lib", r"\s+8\s+0\s+100%")

def test_run_coverage_without_combine(self):
"""Check coverage without combine"""
proc = self.runInProc("scenario/coverage_multiprocessing_without_combine", "-v")
self.assertProcOutputPattern(proc, "lib", r"\s+8\s+1\s+88%")

0 comments on commit 3de648a

Please sign in to comment.