How does IntelliJ find the tests to run?

How does IntelliJ figure out where the tests are and which ones to run?

When I use sbt test from the UNIX command line at the project top level directory, all my tests run. But interactively in IntelliJ they don’t, in fact IntelliJ doesn’t find any tests unless I point the mouse to the actutal file containing the tests.

If I point the mouse to the GreetingTestSuite file in the file browser, I can run GreetingTestSuite, and it runs correctly.

However, if I point the mouse to the lecture-1 directory in the browser wiget…,

Screenshot 2020-05-13 at 17.45.56

it says no tests are found.

I also notice the scala directory appears green while the lecture-1 icon appears black. I’m not sure whether this is related.

I tried looking at the Edit Tests in menu item.

But I don’t see anything that will help.

I see I’ve already asked a similar question some time ago. Nobody seemed to know much about it at that time. How to run tests in IntelliJ

This may be a bug, but I have an idea what may cause it. Look at the folder icons in this screenshot:

lecture-1 to 4 use this one folder which is for simple folders, while other and projects use this one Package, which signifies a package.

This looks to me like Intellij does enforce folder names to be valid package names (which aren’t allowed to contain -), although Scala doesn’t require that. Maybe that causes it to not look in these folders?

1 Like

It’s a reasonable guess that the - in the file name is confusing IntelliJ, but alas it doesn’t seem to be the case. If I change the directory name to lecture1 without the -, it still cannot find any tests. IntelliJ seems to search for tests in the package named lecture rather than in the directory named lecture1.

I fully realize my opinion is a minority opinion. But I really don’t like the idea that package name needs to match the directory hierarchy.

When I eliminate the lecture-1 through lecture-4 directories the tests all run. But now it will probably be difficult for students to figure out which test file corresponds to which homework assignment.

Did you mark the directory as a test-sources directory after you changed the name? Does a project reimport find the tests after renaming?

1 Like

It seems (from my experimentation) that re-import does not help.

How can I mark it as test source?
I don’t see any option to mark as test source.
Screenshot 2020-05-14 at 09.01.59

Oh, I have many more options available (at least in the projects I have open right now)…

1 Like

:sob:

The green color means it’s a test source root. Children will all be searched for tests if they’re not excluded.

The problem stems from hyphens not being valid characters in package names. IntelliJ will create such a package for you, but things start breaking after that.

https://docs.oracle.com/javase/tutorial/java/package/namingpkgs.html

In some cases, the internet domain name may not be a valid package name. This can occur if the domain name contains a hyphen…

Edit: in case there’s a “but that’s Java!” response: https://docs.scala-lang.org/style/naming-conventions.html

Scala packages should follow the Java package naming conventions:

If you are using package names containing backticks, that seems to break the Scala plugin.

https://youtrack.jetbrains.com/issue/IDEA-130684

It seems like this is an IntelliJ bug. I removed the entire work area from disk, and re-cloned from git. In the git repo, I don’t have any of the the InteliJ cache files or compiled libraries–just the source code. After the re-clone finished, I can now run all the tests.