Determining if a compile task and/or source set is for test

What is the best way to determine if a compile task is for test code?

The best potential solution I’ve thought of so far is:

val compileMyTestJava by tasks.getting
val testClasses       by tasks.getting

testClasses.taskDependencies.getDependencies(testClasses).contains(compileMyTestJava)

Also, is there any way to determine if a SourceSet is for test code?

Should I associate the SourceSet with a Task via naming conventions, and then use the Task test heuristic?

Or should I associate them via SourceSet directories & Task#inputs?

I’ve seen SourceSet#output#outputDirectories#builtBy, but the outputDirectories field doesn’t seem to be easily accessible.