Can't run test that is not in test class

Issue #654 resolved
Jeff Mather created an issue

Hi Scott, Among the tests I am trying to get passing, there are some which are part of the code file they are testing, rather than being in a separate test class. When I try to run any of these tests, I get the following result:

no tests were found.jpg

I can run these tests fine through the force.com dev console. Is there some reason IC can't find and run them?

Comments (20)

  1. Scott Wells repo owner

    Jeff, I'm a bit confused by this issue. When I try to add a test method (either with @IsTest or testMethod) to a non-test class, I get the error "Defining type for testMethod methods must be declared as IsTest" from the server. Can you elaborate on how this is working for you and why you'd want test methods in non-test classes?

  2. Jeff Mather reporter

    That's the way some of the tests came in the project I inherited. They run okay from within the Salesforce developer console. I was hoping to be able to run them from IC, as well.

  3. Scott Wells repo owner

    Okay, I checked in with the folks at Salesforce got the full scoop. Evidently @IsTest-annotated methods can only exist in @IsTest-annotated classes, but methods with the testMethod modifier can exist in non-test classes. So that's definitely a gap in IC and I'll use this issue to track the resolution. It's worth mentioning that Salesforce did advise moving all test methods into test classes for a variety of reasons, the main one being proper separation of concerns. Nonetheless, I'll take care of this in IC in an upcoming release.

  4. Justin Julicher

    Hi Scott,

    I have encountered this issue as well - have you made any progress on this?

    I'm trying to run all tests and a lot of legacy code have the tests inline with the class file - so I can't run all tests from within Webstorm....

    thanks

  5. Scott Wells repo owner

    Hey, guys. I have a dumb question here...I started on a fix for this by moving one of my test methods into the corresponding production class, changing @IsTest to testMethod. When I tried to save the class, it failed with:

    Error:(57, 28) Defining type for testMethod methods must be declared as IsTest

    If I do in fact annotate the containing class as @IsTest, I'm able to save and I'm able to run the test method successfully. However, if memory serves, the original issue was based on production classes containing testMethod tests where the parent class is not itself annotated as a test class.

    I also tried dropping the API version back to 30.0 to see if perhaps this is a behavior change based on API version, but the same error was reported. Perhaps 30.0 isn't far enough back?

    Can you please confirm the correct way to reproduce this behavior? Again, sorry what is likely a dumb question, but once I can reproduce the scenario, it should be quick to address. Thanks!

  6. Jon Wu

    Try going even further back. It looks like v28 was the first to require isolation https://developer.salesforce.com/blogs/engineering/2013/04/apex-test-code-segregation.html.

    There was also a big change in v24 to isolate test data https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_data_access.htm.

    We have a bunch of stuff < v24, and these 2 big changes to testing make it harder for us to just switch to a newer API version without refactoring. Even though it's refactoring that should be done... if it ain't broke, don't fix it, is generally the way we go.

  7. Scott Wells repo owner

    Thanks, Jon. v28 didn't get me there but v24 did. I'll try to include a fix for this in the next build.

  8. Jon Wu

    This is awesome. Thank you!

    For future reference, versions 24 and 28 are the versions where things changed. You'd have to go to 23 to see all data by default and to version 27 to avoid test code segregation, which is why 24 did the trick I think.

  9. Scott Wells repo owner

    Delivered in 1.8.4.4 and 2.0.4.9. Please let me know if you have any issues running these tests now.

  10. Justin Julicher

    Hi Scott,

    Sorry this isn't completely resolved.

    Classes can also have inline test methods annotated with IsTest which IC is still not picking up....

    thanks

  11. Scott Wells repo owner

    Interesting. I was under the impression from Salesforce that only testMethod was allowed in non-@IsTest-annotated classes. Easy enough to fix, but not what I'd understood. I'll verify the behavior, implement the additional change, and include it in the next build. Right now I'm shooting for tomorrow morning for its release.

  12. Scott Wells repo owner

    Additional fix delivered with 2.0.5.0 and 1.8.4.5. Let me know if this doesn't resolve the issue completely for you guys.

  13. Log in to comment