diff mbox series

[1/3] kunit: tool: make TestCounts a dataclass

Message ID 20221102164005.2516646-1-dlatypov@google.com (mailing list archive)
State Accepted
Commit f473dd9488d910aab109e8c6a2e4181125ca322a
Delegated to: Brendan Higgins
Headers show
Series [1/3] kunit: tool: make TestCounts a dataclass | expand

Commit Message

Daniel Latypov Nov. 2, 2022, 4:40 p.m. UTC
Since we're using Python 3.7+, we can use dataclasses to tersen the
code.

It also lets us create pre-populated TestCounts() objects and compare
them in our unit test. (Before, you could only create empty ones).

Signed-off-by: Daniel Latypov <dlatypov@google.com>
---
 tools/testing/kunit/kunit_parser.py    | 25 ++++++++-----------------
 tools/testing/kunit/kunit_tool_test.py |  4 +---
 2 files changed, 9 insertions(+), 20 deletions(-)


base-commit: 5aaef24b5c6d4246b2cac1be949869fa36577737

Comments

David Gow Nov. 3, 2022, 6:37 a.m. UTC | #1
On Thu, Nov 3, 2022 at 12:40 AM Daniel Latypov <dlatypov@google.com> wrote:
>
> Since we're using Python 3.7+, we can use dataclasses to tersen the
> code.
>
> It also lets us create pre-populated TestCounts() objects and compare
> them in our unit test. (Before, you could only create empty ones).
>
> Signed-off-by: Daniel Latypov <dlatypov@google.com>
> ---

Looks good, thanks.

Reviewed-by: David Gow <davidgow@google.com>

Cheers,
-- David

>  tools/testing/kunit/kunit_parser.py    | 25 ++++++++-----------------
>  tools/testing/kunit/kunit_tool_test.py |  4 +---
>  2 files changed, 9 insertions(+), 20 deletions(-)
>
> diff --git a/tools/testing/kunit/kunit_parser.py b/tools/testing/kunit/kunit_parser.py
> index 1ae873e3e341..f022966858f2 100644
> --- a/tools/testing/kunit/kunit_parser.py
> +++ b/tools/testing/kunit/kunit_parser.py
> @@ -10,6 +10,7 @@
>  # Author: Rae Moar <rmoar@google.com>
>
>  from __future__ import annotations
> +from dataclasses import dataclass
>  import re
>  import sys
>
> @@ -67,27 +68,17 @@ class TestStatus(Enum):
>         NO_TESTS = auto()
>         FAILURE_TO_PARSE_TESTS = auto()
>
> +@dataclass
>  class TestCounts:
>         """
>         Tracks the counts of statuses of all test cases and any errors within
>         a Test.
> -
> -       Attributes:
> -       passed : int - the number of tests that have passed
> -       failed : int - the number of tests that have failed
> -       crashed : int - the number of tests that have crashed
> -       skipped : int - the number of tests that have skipped
> -       errors : int - the number of errors in the test and subtests
> -       """
> -       def __init__(self):
> -               """Creates TestCounts object with counts of all test
> -               statuses and test errors set to 0.
> -               """
> -               self.passed = 0
> -               self.failed = 0
> -               self.crashed = 0
> -               self.skipped = 0
> -               self.errors = 0
> +       """
> +       passed: int = 0
> +       failed: int = 0
> +       crashed: int = 0
> +       skipped: int = 0
> +       errors: int = 0
>
>         def __str__(self) -> str:
>                 """Returns the string representation of a TestCounts object."""
> diff --git a/tools/testing/kunit/kunit_tool_test.py b/tools/testing/kunit/kunit_tool_test.py
> index e2cd2cc2e98f..9fa4babb2506 100755
> --- a/tools/testing/kunit/kunit_tool_test.py
> +++ b/tools/testing/kunit/kunit_tool_test.py
> @@ -179,9 +179,7 @@ class KUnitParserTest(unittest.TestCase):
>                                 kunit_parser.extract_tap_lines(
>                                 file.readlines()))
>                 # A missing test plan is not an error.
> -               self.assertEqual(0, result.counts.errors)
> -               # All tests should be accounted for.
> -               self.assertEqual(10, result.counts.total())
> +               self.assertEqual(result.counts, kunit_parser.TestCounts(passed=10, errors=0))
>                 self.assertEqual(
>                         kunit_parser.TestStatus.SUCCESS,
>                         result.status)
>
> base-commit: 5aaef24b5c6d4246b2cac1be949869fa36577737
> --
> 2.38.1.273.g43a17bfeac-goog
>
diff mbox series

Patch

diff --git a/tools/testing/kunit/kunit_parser.py b/tools/testing/kunit/kunit_parser.py
index 1ae873e3e341..f022966858f2 100644
--- a/tools/testing/kunit/kunit_parser.py
+++ b/tools/testing/kunit/kunit_parser.py
@@ -10,6 +10,7 @@ 
 # Author: Rae Moar <rmoar@google.com>
 
 from __future__ import annotations
+from dataclasses import dataclass
 import re
 import sys
 
@@ -67,27 +68,17 @@  class TestStatus(Enum):
 	NO_TESTS = auto()
 	FAILURE_TO_PARSE_TESTS = auto()
 
+@dataclass
 class TestCounts:
 	"""
 	Tracks the counts of statuses of all test cases and any errors within
 	a Test.
-
-	Attributes:
-	passed : int - the number of tests that have passed
-	failed : int - the number of tests that have failed
-	crashed : int - the number of tests that have crashed
-	skipped : int - the number of tests that have skipped
-	errors : int - the number of errors in the test and subtests
-	"""
-	def __init__(self):
-		"""Creates TestCounts object with counts of all test
-		statuses and test errors set to 0.
-		"""
-		self.passed = 0
-		self.failed = 0
-		self.crashed = 0
-		self.skipped = 0
-		self.errors = 0
+	"""
+	passed: int = 0
+	failed: int = 0
+	crashed: int = 0
+	skipped: int = 0
+	errors: int = 0
 
 	def __str__(self) -> str:
 		"""Returns the string representation of a TestCounts object."""
diff --git a/tools/testing/kunit/kunit_tool_test.py b/tools/testing/kunit/kunit_tool_test.py
index e2cd2cc2e98f..9fa4babb2506 100755
--- a/tools/testing/kunit/kunit_tool_test.py
+++ b/tools/testing/kunit/kunit_tool_test.py
@@ -179,9 +179,7 @@  class KUnitParserTest(unittest.TestCase):
 				kunit_parser.extract_tap_lines(
 				file.readlines()))
 		# A missing test plan is not an error.
-		self.assertEqual(0, result.counts.errors)
-		# All tests should be accounted for.
-		self.assertEqual(10, result.counts.total())
+		self.assertEqual(result.counts, kunit_parser.TestCounts(passed=10, errors=0))
 		self.assertEqual(
 			kunit_parser.TestStatus.SUCCESS,
 			result.status)