Netbeans: How to alter naming pattern used to identify testSuites
I have numerous JUNIT TestSuites that have a "开发者_如何转开发Tests" suffix. Eclipse recognises the static suite() method and makes a run test option available in package explorer. However the same is not true with NetBeans which only gives me the default Run context menu option. However i noticed that it does recognize any class that uses "TestSuite" as a suffix as a Tests suite. I am new to NB 7.0 and am unable to find out how to alter the pattern or some how make NB work in the same way as Eclipse.
Ok, actually, I figured it out, while digging around trying to solve my own problem. Vanilla NetBeans uses ant, so this question is actually an ant question. If you look in the project's directory, we see that there is the build.xml file. This is the primary file that instructs ant what to do. If we look inside the file, we see that it's primary function is to simply <import /> the nbproject/build-impl.xml file. So, what we need is in here. We can use this file to override the functionality of the default implementation.
If you dig down into the build-impl.xml file, you'll come upon the junit section. Look for this section:
<target depends="init,compile-test,-pre-test-run" if="have.tests" name="-do-test-run">
<j2seproject3:junit testincludes="**/*Test.java"/>
</target>
This is the section that actually runs the tests, and it's in a small block of xml, which is exactly what we need, because we're only trying to override one small bit. As is hopefully obvious, you can see we're only matching (recursively) file names that match the patter "*Test.java". So, in your case you would edit (or add) to that the pattern "TestSuite.java". Don't add this line to that file though, it might get hosed, as this is an automatically generated file. Instead, copy that whole bit to the actual build.xml file, and I think it needs to be pasted above the <import /> directive so it will take precedence. (Maybe not, try it below as well if that doesn't work.)
Now, here's the tricky bit, NetBeans has a custom junit runner, the <j2seproject3:junit /> tag. Without digging down even further to see how to pass it arguments, we don't know exactly how to add arguments to it, but for your case, this is fine, we don't really need to add one, we can make it work by just editing the one.
<target depends="init,compile-test,-pre-test-run" if="have.tests" name="-do-test-run">
<j2seproject3:junit testincludes="**/*Test*.java"/>
</target>
This should likely do it for you. Now it will match anything that has the word test in it, and not just stuff that ends with the word test.
If you're using an ant project, you need to go into the ant files in the project directory, and find the ant commands that locate the tests. Last time I checked (a couple of years ago), the file name pattern that it was looking for was hard coded.
精彩评论