Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@Monilnarang
Copy link
Contributor

@Monilnarang Monilnarang commented Feb 18, 2025

  • I have registered the PR changes.

Ⅰ. Describe what this PR did

Summary:

  • Created 2 separate parameterized tests for testParse
  • Parameterized testParseThrowException

Aim:
Improve the test code by avoiding code duplication, improving maintainability, and enhancing readability. By converting the test into a parameterized unit test, we reduce boilerplate code, make it easier to extend by simply adding new input values, and improve debugging by clearly identifying which test case fails instead of searching through individual assertions.

Elaboration:
Like in the below test from DurationUtilTest.java:

@Test
    public void testParse() {
        Assertions.assertEquals(-1L, DurationUtil.parse("").getSeconds());
        Assertions.assertEquals(0L, DurationUtil.parse("8").getSeconds());
        Assertions.assertEquals(8L, DurationUtil.parse("8").toMillis());
        Assertions.assertEquals(0L, DurationUtil.parse("8ms").getSeconds());
        Assertions.assertEquals(8L, DurationUtil.parse("8ms").toMillis());
        Assertions.assertEquals(8L, DurationUtil.parse("8s").getSeconds());
        Assertions.assertEquals(480L, DurationUtil.parse("8m").getSeconds());
        Assertions.assertEquals(28800L, DurationUtil.parse("8h").getSeconds());
        Assertions.assertEquals(691200L, DurationUtil.parse("8d").getSeconds());

        Assertions.assertEquals(172800L,DurationUtil.parse("P2D").getSeconds());
        Assertions.assertEquals(20L,DurationUtil.parse("PT20.345S").getSeconds());
        Assertions.assertEquals(20345L,DurationUtil.parse("PT20.345S").toMillis());
        Assertions.assertEquals(900L,DurationUtil.parse("PT15M").getSeconds());
        Assertions.assertEquals(36000L,DurationUtil.parse("PT10H").getSeconds());
        Assertions.assertEquals(8L,DurationUtil.parse("PT8S").getSeconds());
        Assertions.assertEquals(86460L,DurationUtil.parse("P1DT1M").getSeconds());
        Assertions.assertEquals(183840L,DurationUtil.parse("P2DT3H4M").getSeconds());
        Assertions.assertEquals(-21420L,DurationUtil.parse("PT-6H3M").getSeconds());
        Assertions.assertEquals(-21780L,DurationUtil.parse("-PT6H3M").getSeconds());
        Assertions.assertEquals(21420L,DurationUtil.parse("-PT-6H+3M").getSeconds());
    }
  • The same method calls (DurationUtil.parse("someString").getSeconds(), DurationUtil.parse("someString").toMillis()) are repeated multiple times with different inputs, making the test harder to maintain and extend.
  • Both the above method calls are cluttered together in one test, blurring the exact scope of this test.
  • When a test fails, JUnit only shows which assertion failed, but not which specific input caused the failure.
  • Adding new test cases requires copying and pasting another Assertions.assertEquals(...) statement instead of simply adding new data.

To accomplish this, I have split this test into 2 separate tests (testParseGetSeconds and testParseToMillis) and retrofitted them into a parameterized unit test. This reduces duplication, allows easy extension by simply adding new value sets, and makes debugging easier as it clearly indicates which test failed instead of requiring a search through individual assertions.

Test execution results before changes:
Screenshot 2025-02-17 at 2 38 03 PM

Test execution results after changes:
Screenshot 2025-02-17 at 2 37 42 PM

Ⅱ. Does this pull request fix one issue?

Ⅲ. Why don't you add test cases (unit test/integration test)?

Ⅳ. Describe how to verify it

Successful tests run

Ⅴ. Special notes for reviews

@codecov
Copy link

codecov bot commented Feb 18, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 52.17%. Comparing base (46c9b63) to head (2334812).
Report is 2 commits behind head on 2.x.

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff              @@
##                2.x    #7167      +/-   ##
============================================
- Coverage     52.18%   52.17%   -0.02%     
  Complexity     6813     6813              
============================================
  Files          1154     1154              
  Lines         41112    41112              
  Branches       4818     4818              
============================================
- Hits          21455    21449       -6     
- Misses        17617    17624       +7     
+ Partials       2040     2039       -1     

see 5 files with indirect coverage changes

Copy link
Contributor

@funky-eyes funky-eyes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Monilnarang
Copy link
Contributor Author

Monilnarang commented Feb 19, 2025

Hi @funky-eyes,
Thanks for the review and the approval.
I saw the build (8) failed, it seems flaky. Should we rerun it or is it good to be merged?

Copy link
Member

@xingfudeshi xingfudeshi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@xingfudeshi xingfudeshi merged commit ea9a8a2 into apache:2.x Feb 19, 2025
8 checks passed
@slievrly slievrly added this to the 2.4.0 milestone Mar 9, 2025
slievrly pushed a commit to slievrly/fescar that referenced this pull request Oct 21, 2025
YvCeung pushed a commit to YvCeung/incubator-seata that referenced this pull request Dec 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants