I'm attempting to do unit and integration tests for my application.
I'm using Microsoft.VisualStudio.TestTools.UnitTesting;
The generated class that I refactored works just fine and all the tests run.
I have added additional classes to the project and none of the tests in those files are detected by the test explorer.
As you can see, DatabaseConnectionTests is running just fine but CourseInformation tests isn't
I have checked to make sure the test methods have '[TestMethod]' above them, they return void and have no parameters just like the methods in DatabaseConnectionTests that run.
This is quite embarrassing, but after looking at the image, I realised my classes were not public.
Going to leave this up in case anyone else overlooks this simple mistake.
Related
Context
I have a project with following traits
IntelliJ Ultimate 2020.1
Java 13 (with module-info.java)
Kotlin 1.3.72
JUnit (+ truth)
maven (I believe this to be unimportant)
The code base is mixed, some classes are written using plain Java, others with Kotlin, the same is true for tests. Everything works as expected, that is
all code is compiled in proper order and fully interoperable between Kotlin and Java
all test can be executed using either mvn test or IntelliJ "Run Test"
the resulting jar can be run (for the sake of providing context)
but...
apart from the fact that everything works, IntelliJ warns me about a non declared module dependency only if the test class is written in Kotlin. This warning is not displayed for test classes written in plain Java.
The warning:
Error:(9, 6) Symbol is declared in module 'org.junit.jupiter.api' which current module does not depend on
That warning normally allows one to import / require the respective module / dependency, but there is no solution offered in [alt]+[enter] dialog.
Things I have tried so far:
upgrading from JUnit 4 to 5 didn't change the situation
googling to no avail :(
making sure tests written in Kotlin are really executed when mvn test is run by making a test fail
manually running test using IntelliJ "Run Test"
converting tests back and forth from / to Kotlin
explicitly requiring the the JUnit API / truth in module-info
The latter obviously prevents the warning but is no solution since that actually produces a hard dependency. From what I found out while googling, the maven-surefire-plugin makes sure the test-dependencies are included. Also: running mvn test works like charm as stated above, so this does not seem to be part of the problem.
Seeing all the red lines when writing test is really annyoing...
the suspect behavior
same test but in java - everything is fine
Question:
How can I fix that warning for Kotlin Test Classes in IntelliJ?
Note
I have come to believe this is a bug in IntelliJ but I'd be happy to be shown what I overlooked.
Since everything from compiling to running with Maven works like a charm, I excluded details regarding project structure and so on. The issue at hand is about the warning in the IntelliJ, not about a broken build or non-functional jars. I'll glady add those in case they turn out to be necessary.
also since everthing actually works (apart from the annoying warning), I really don't know where to continue researching and hence created a question.
This is a bug in IDEA Kotlin plugin error highlighting: https://youtrack.jetbrains.com/issue/KT-26037
Workaround: add #file:Suppress("JAVA_MODULE_DOES_NOT_DEPEND_ON_MODULE") to the test file before the package declaration.
Note: Answering my own problem for future developers.
I right click a Method > Create Intelli Tests.
I choose the MSTestv2, chose the Project.Test project, click OK and see the following output:
Processing Proj.API
Scanning assembly references in Proj.API Applying
template AssemblyInfo to Proj.API Applying template Tests to Proj.API
test stubbing ProductController -> ProductControllerIntelliTest
generating method bodies
flushing generated code
Unfortunately the Test.cs file is NOT created.
I've set all projects x86 and that doesn't make a difference.
Does anyone know why this doesn't work?
For some reason I had to specify a New Project for the Intelli Tests.
Creating new IntelliTests in the new project work, however they cant be added to the standard Test Project.
Hopefully someone else knows why.
I'm trying to use test support classes within my tests. I want these classes to be available for all different test types.
My directory structure is as follows;
/test/functional
/test/integration
/test/unit
/test/support
I have test helper classes within the /test/support folder that I would like to be available to each of the different test types.
I'm using GGTS and I've added the support folder to the classpath. But whenever I run my integration tests running 'test-app' I get a compiler 'unable to resolve class mypackage.support.MyClass
When I run my unit tests from within GGTS the support classes are found and used. I presume this is because the integration tests run my app in its own JVM.
Is there any way of telling grails to include my support package when running any of my tests?
I don't want my test support classes to be in my application source folders.
The reason that it works for your unit tests inside the IDE is that all source folders get compiled into one directory, and that is added to your classpath along with the jars GGTS picks up from the project dependencies. This is convenient but misleading, because it doesn't take into account that Grails uses different classpaths for run-app and each of the test phases, which you see when you run the integration tests. GGTS doesn't really run the tests; it runs the same grails test-app process that you do from the commandline, and captures its output and listens for build events so it can update its JUnit view.
It's possible to add extra jar files to the classpath for tests because you can hook into an Ant event and add it to the classpath before the tests start. But the compilation process is a lot more involved and it looks like it would be rather ugly/hackish to get it working, and would likely be brittle and stop working in the future when the Grails implementation changes.
Here are some specifics about why it'd be non-trivial. I was hoping that you could call GrailsProjectTestCompiler.compileTests() for your extra directory, but you need to compile it along with the test/unit directory for unit tests and the test/integration directory for integration tests, and the compiler (GrailsProjectTestCompiler) presumes that each test phase only needs to compile that one directory. That compiler uses Gant, and each test phase has its own Grailsc subclass (org.grails.test.compiler.GrailsTestCompiler and org.grails.test.compiler.GrailsIntegrationTestCompiler) registered as taskdefs. So it should be possible to subclass them and add logic to compile both the standard directory and the shared directory, and register those as replacements, but that requires also subclassing and reworking GrailsProjectTestRunner (which instantiates GrailsProjectTestCompiler), and hooking into an event to replace the projectTestRunner field in _GrailsTest.groovy with your custom one, and at this point my brain hurts and I don't want to think about this anymore :)
So instead of all this, I'd put the code in src/groovy and src/java, but in test-specific packages that make it easy to exclude the compiled classes from your WAR files. You can do that with a grails.war.resources closure in BuildConfig.groovy, e.g.
grails.war.resources = { stagingDir ->
println '\nDeleting test classes\n'
delete(verbose: true) {
// adjust as needed to only delete test-specific classes
fileset dir: stagingDir, includes: '**/test/**/*.class'
}
println '\nFinished deleting test classes\n'
}
We are writing unit tests for our business layer running under .NET 4.0. The business layer is a straightforward C# class library that usually runs within SOAP and REST web services. Our application uses log4net within a separate wrapper assembly for logging. The C# code in the logging assembly has an assembly info directive that tells log4net the name of the configuration file, a la-
[assembly: log4net.Config.XmlConfigurator(ConfigFile="AcmeLogging.config", Watch=true)]
Initializing the log4net through the wrapper works fine in the web services. When we initialize it from with our unit test assembly it does not appear to see the configuration file. The configuratino file is configured through properties to be copied to the execution directory, and we do see it in the bin\debug directory. A quick console test program using the logging assembly running from within that same folder works fine. The curious thing is that the behavior problems are intermittent, and pop up on different developers' machines at different times and cannot be cured in any deterministic way.
Stepping through the wrapper assembly code, the log4netLogManager.GetLogger() call appears to return correctly, but the list of appenders returned by log.Logger.Repository.GetAppenders() is empty. Since this incorrect behavior is the same whether the file is in the Bin\Debug folder or not, we believe that it is not seeing the file.
Any clues as to what we're missing about running log4net in the Microsoft Test Framework would be greatly appreciated.
Unit tests are unique in the fact that if you need configuration files in your unit tests you need to include them as deployment items. Here is an example of how I do this within my test class:
[TestClass]
[DeploymentItem("hibernate.cfg.xml")]
public class AsyncForwardingAppenderTest
{
}
In addition to the Deployment attribute you need to Enable Deployment in your test settings. To do this go to Test->Edit Test Settings->. Then click on Deployment area on right. Click the checkbox Enable Deployment.
After doing this and running your test your config file should be located in your test results folder. TestResults\username_machine date stamp\Out. If your config file is not located in this folder it will not work. This is what the DeploymentItem attribute does. It sticks the file in this Out folder.
If you don't want to include the DeploymentItem attribute on every test class what I did was to create a base test class that all of the tests that use log4net inherit from and mark it with the DeploymentItem attribute.
I have an assembly that needs to be in the test output dir for my tests to run.
I have the assembly referenced as a local copy in the project but on the build server this gets ignored.
The two ways I have found to fix this are
Add a special attribute to test method that will make sure the file is there for each test.
[DeploymentItem("my assembly")]
This is not very practical as this assembly is required for almost every test in the assembly.
Add test run config file with special deployment section. I am using a TestContainer in my build scripts to run the tests I think that this may be the reason my included test run config does not get picked up and the assembly not copied. I would prefer to not have a vsmdi test list file as I am trying to run all tests and I feel this would be a violation of DRY.
Any suggestions on how I can get my tests running?
As my assembly was being dynamicly loaded the unit test framework was not copying it.
I added a explict refrence to it by calling typeof on one of the types in the assembly and all is fine.
Thanks Jerome Laban for your help with this one.