Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added new duplicated_test_run cop #164

Conversation

ignacio-chiazzo
Copy link
Contributor

@ignacio-chiazzo ignacio-chiazzo commented Mar 26, 2022

Added new duplicated_test_run cop

If a Minitest class inherits from another class, it will also inherit its methods causing Minitest to run the parent's tests methods twice.

This PR detects when there are two tests classes, one inherits from the other, and both have tests methods. MinitestRubocop will add an offence to the Child class in such a case.

For example:

class B < Minitest::Test
  test "test 1" do 
      #....
  end

  class A < B
    test "test 2" do 
        #....
    end 
  end
end

It will run test_1 twice!

I came across a few PRs/pieces of code using subclassing. I wrote a Gem that detects duplicated test run during Runtime and a Post with more details.

1_mKcZFLz8LpZHmxI3Qrf_vA

This cop has some limitations:

  • It only works if both classes are on the same file.
  • Using describes also has the same problem, and the cop doesn't detect it (yet ;)).

More information in this post.

@ignacio-chiazzo ignacio-chiazzo changed the title Remove having multiple CHANGELOG files from the CONTRIBUTING README Added new duplicated_test_run cop Mar 26, 2022
@ignacio-chiazzo ignacio-chiazzo force-pushed the ignacio-chiazzo/add-duplicated-test-run-cop branch 2 times, most recently from 1e385a4 to 13d7a9e Compare March 26, 2022 22:34
config/default.yml Outdated Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
config/default.yml Outdated Show resolved Hide resolved
@ignacio-chiazzo ignacio-chiazzo force-pushed the ignacio-chiazzo/add-duplicated-test-run-cop branch from 13d7a9e to df5ec71 Compare March 29, 2022 23:09
@koic
Copy link
Member

koic commented Mar 30, 2022

Nice! Can you squash your commits into one?

@ignacio-chiazzo ignacio-chiazzo force-pushed the ignacio-chiazzo/add-duplicated-test-run-cop branch 2 times, most recently from a621b20 to 84a390e Compare March 30, 2022 17:13
If a Minitest class inherits from another class, it will also inherit its methods causing Minitest to run the parent’s tests methods twice.

This PR detects when there are two tests classes, one inherits from the other, and both have tests methods.  MinitestRubocop will add an offence to the Child class in such a case.

Note that this cop only works if both classes are on the same file.
@ignacio-chiazzo ignacio-chiazzo force-pushed the ignacio-chiazzo/add-duplicated-test-run-cop branch from 84a390e to 98ebb10 Compare March 30, 2022 17:14
@koic koic merged commit 582727c into rubocop:master Mar 31, 2022
@koic
Copy link
Member

koic commented Mar 31, 2022

Thanks!

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.

3 participants