{"action":"created","issue":{"url":"https://api.github.com/repos/ysbaddaden/minitest.cr/issues/6","labels_url":"https://api.github.com/repos/ysbaddaden/minitest.cr/issues/6/labels{/name}","comments_url":"https://api.github.com/repos/ysbaddaden/minitest.cr/issues/6/comments","events_url":"https://api.github.com/repos/ysbaddaden/minitest.cr/issues/6/events","html_url":"https://github.com/ysbaddaden/minitest.cr/issues/6","id":107726386,"number":6,"title":"All tests in a test class should not be run on a single instance of Minitest::Test","user":{"login":"reggieb","id":213040,"avatar_url":"https://avatars.githubusercontent.com/u/213040?v=3","gravatar_id":"","url":"https://api.github.com/users/reggieb","html_url":"https://github.com/reggieb","followers_url":"https://api.github.com/users/reggieb/followers","following_url":"https://api.github.com/users/reggieb/following{/other_user}","gists_url":"https://api.github.com/users/reggieb/gists{/gist_id}","starred_url":"https://api.github.com/users/reggieb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/reggieb/subscriptions","organizations_url":"https://api.github.com/users/reggieb/orgs","repos_url":"https://api.github.com/users/reggieb/repos","events_url":"https://api.github.com/users/reggieb/events{/privacy}","received_events_url":"https://api.github.com/users/reggieb/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/ysbaddaden/minitest.cr/labels/bug","name":"bug","color":"fc2929"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-09-22T14:28:45Z","updated_at":"2016-01-04T19:28:18Z","closed_at":null,"body":"With ruby minitest, each test within a test file, is run on a new instance of the test class. This prevents instance variables set in one test leaking into another test. \r\n\r\nFor example, in ruby minitest both of these tests would pass:\r\n\r\n    class MyTest < Minitest::Test\r\n\r\n      def test_a\r\n        assert_equal :a, a\r\n        @a = :b\r\n      end\r\n\r\n      def test_b\r\n        assert_equal :a, a\r\n        @a = :c\r\n      end\r\n\r\n      def a\r\n        @a ||= :a\r\n      end\r\n\r\n    end\r\n\r\nWith this crystal minitest suite one of the tests will always fail.\r\n\r\nI think the issue is to do with the macro [Minitest::Test#run_tests](https://github.com/ysbaddaden/minitest.cr/blob/master/src/minitest/test.cr) being an instance method rather than a class method. \r\n\r\nI believe the process should be:\r\n\r\n1. get test class\r\n2. find instance methods starting with \"test_\"\r\n3. for each test method\r\n  * get a test instance\r\n  * call the test method on the test instance\r\n\r\nRather than the current:\r\n\r\n1. get test class\r\n2. get test instance\r\n3. find instance methods starting with \"test_\"\r\n4. for each test method\r\n  * call the test method on the test instance\r\n"},"comment":{"url":"https://api.github.com/repos/ysbaddaden/minitest.cr/issues/comments/168777863","html_url":"https://github.com/ysbaddaden/minitest.cr/issues/6#issuecomment-168777863","issue_url":"https://api.github.com/repos/ysbaddaden/minitest.cr/issues/6","id":168777863,"user":{"login":"reggieb","id":213040,"avatar_url":"https://avatars.githubusercontent.com/u/213040?v=3","gravatar_id":"","url":"https://api.github.com/users/reggieb","html_url":"https://github.com/reggieb","followers_url":"https://api.github.com/users/reggieb/followers","following_url":"https://api.github.com/users/reggieb/following{/other_user}","gists_url":"https://api.github.com/users/reggieb/gists{/gist_id}","starred_url":"https://api.github.com/users/reggieb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/reggieb/subscriptions","organizations_url":"https://api.github.com/users/reggieb/orgs","repos_url":"https://api.github.com/users/reggieb/repos","events_url":"https://api.github.com/users/reggieb/events{/privacy}","received_events_url":"https://api.github.com/users/reggieb/received_events","type":"User","site_admin":false},"created_at":"2016-01-04T19:28:18Z","updated_at":"2016-01-04T19:28:18Z","body":"It's this bit of code that is the problem:\r\n\r\n    macro def run_tests : Nil\r\n          {{\r\n            @type.methods\r\n              .map(&.name.stringify)\r\n              .select(&.starts_with?(\"test_\"))\r\n              .shuffle\r\n              .map { |m| \"run_one(#{m}) { #{m.id} }\" }\r\n              .join(\"\\n\")\r\n              .id\r\n          }}\r\n          nil\r\n     end\r\n\r\nIt's calling run_one on the the same test class instance. If this was ruby, you could implement a dirty fix by adding a class method `run_one`.\r\n\r\n    def self.run_one(method, &process)\r\n      new.run_one(method, &process)\r\n    end\r\n\r\nThen this line:\r\n\r\n     .map { |m| \"run_one(#{m}) { #{m.id} }\" }\r\n\r\nCould be changed to:\r\n\r\n     .map { |m| \"self.class.run_one(#{m}) { #{m.id} }\" }\r\n\r\nThat way each test would be run on a new instance of the test class. Longer term, I'd move the process of initiating the test to be a class method, and then have that generate a new instance against which to call run_one.\r\n\r\nHowever, I'm not sure of the best way of doing it in Crystal."}}