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

Limit spec dsl defined classes to own test methods #865

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

amckinnie
Copy link

Issue:
Currently, minitest spec styles deals with tests being included from preceding or ancestor classes within the DSL with the method Minitest::Spec::DSL#nuke_test_methods!. This will wipe out existing test methods that have been defined at the time of class initialization (Minitest::Spec::DSL#create). However, when tests are defined after a block is declared, they are still included when the instance methods for the class are pulled (since they're pulled with all the ancestor instance methods.

In this example:

class MyTest < Minitest::Test
  describe "foo" do end
  def test_name; end
end

test_name will be run twice, once as part of class MyTest and once as part of class MyTest::foo. This will occur for each describe block (or alias), so if there are multiple describe blocks in a test file, it will run for each of them.

Proposed Solution:
For classes defined by the spec DSL, limit test methods pulled to that of the class itself.


def self.methods_matching re
public_instance_methods(false).grep(re).map(&:to_s)
end
Copy link
Collaborator

Choose a reason for hiding this comment

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

This doesn't really make sense here, does it? Maybe I'm confused.

Copy link
Author

Choose a reason for hiding this comment

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

The class created by Class.new will inherit from Minitest:Spec or some other subclass (Minitest::Spec#spec_type). Class.new allows you to run code or create methods for that dynamic subclass which will be defined with the call to describe.

I had initially approached it by changing the method in Runnable, though that would cause issues with inheritance (since we wouldn't want to upset the current expected behavior of a test class inheriting from another running the tests of the inherited class). Thus, we'd need only the classes defined by describe to have this behavior, which is why we'd need this here.

@zenspider
Copy link
Collaborator

zenspider commented Mar 23, 2021 via email

@amckinnie
Copy link
Author

amckinnie commented Mar 23, 2021

Why not in Spec?

We could, though with the inheritance issue still applies.

class ATest < Minitest::Spec
  ...
end

class BTest < ATest
  ...
end

This would currently run everything from ATest in BTest. Would we no longer want to do that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants