Using a config file in Microsoft Unit Tests - visual-studio-2010

Currently for a number of my unit tests I require that some configuration be pulled from a file. Initially I thought I only needed to add Test Settings to my unit testing project and configure this in Test Settings->Deployment. I added my config file here but my unit tests were still complaining that they couldn't find this config file. Finally I found that I needed to add the [DeploymentItem("Test.config")] attribute to any of my unit test classes that needed this.
Is this how it's supposed to be done? It seems like I should not have to use the DeploymentItem attribute. Am I doing something wrong here?

Do you mean that your code under test requires a config file? Then you can add a regular app.config to your unit tests. Well in all other cases you can use that as well.

set the config file to copy to the output directory in a post build

Related

Gradle TestNG how to run all suites without hardcoding them in build.gradle

I am working on a Java TestNG project with gradle
We have multiple xml test suites
In order to run them we pass them as this :
suites ("src/test/resources/sanity.xml",
"src/test/resources/buyside.xml",
"src/test/resources/useraccess.xml",
"src/test/resources/outboundfiles.xml",
"src/test/resources/quicksight.xml")
But everytime I add a new suite to the project I have to add it manually in the build.gradle file
Is there a way to get all the suites in the project?
I searched the documentation for gradle-testng and found a method getSuiteXmlFiles() but i dont know how to use it. If I pass it like
suites (getSuiteXmlFiles())
It does not work.
Is this the correct method to use and how can I use it ? Or if there is another way to fetch all the suites in the project please let me know
Thank you
I found a solution to do it
I defined a list of all xml files in the folder resources
def allSuites = new File("src/test/resources/").listFiles();
Then I passed it to the suites method:
suites allSuites

Web config app settings values null during visual studio unit test

I have added new unit test project in VS 2015 for a method in my .net solution, while i run test case, the web config app settings values are null and unable to complete test, Actually the config key values are available and accessible during web app run, but during unit test the values are null,
can anyone please help me out.
Thanks in Advance
Test project needs app.cofig with similar settings present as in web project being tested.
Unit test is run in a separate app domain so needs it's own config file.
The ConfigurationManager reads the config file of the current app domain being run so create an app.config file for the test project and copy the desired configuration settings over to allow the tests to be exercised as expected.
This issue also exposes how your code is tightly coupled to implementation concerns in terms of the using the ConfigurationManager and you should consider abstracting configuration access so that they can be mocked/replaced to allow unit tests to be done in isolation.

TestNG run/debug from IDEA cannot load main class files

I have inherited some Selenium TestNG tests which I can run with no problem via 'mvn test' but when I try to run/debug inside IDEA I receive an instant error:
Error: Could not find or load main class Files
In my own projects I have xml test suites and I keep my /main empty. I can run tests easily with either command line and inside IDEA.
However, this project structure is different from one I developed myself. Here I have test classes in /test/java directory but all PageObject classes, helpers and such in /main/java. There is no xml suites (I am not sure how it is even working and if it is possible to run certain test class). See picture below.
The question is: how to make those tests run/debug inside IDEA?
As dumb as it sounds I have been passing a parameter to Firefox binary within '' instead of "" resulting
-Dbinary='C:/Program Files/...'
to be interpreted as
-Dbinary =C:/Program
with remaining string treated as class.

IntelliJ IDEA and Maven: creating a test resource in the same package

I'm using IntelliJ IDEA and my project has a default Maven structure.
I often run into the scenario where I need to create a new class or resource in the same package, but in different folder (one of src/main/java, src/main/resources, src/test/java, src/test/directory). The case where I'm editing a class in src/main/java and I need to create a test in src/test/java is covered by the Create Test command.
What about the other scenarios?
Often I find myself writing a unit test and I'd like to create a test resource in the same package in test resources folder. Is there a quick way how to achieve that?
What you can do at least is to copy the path of your package, and then (in the test resources dir) say new directory, and paste the path there. You then need to delete the absolute prefix, but can keep the package definition. With deep package structures, this at least helps a bit.
To the best of my knowledge (via 10 years of use) there is not a built in way to do this. It is something I've recently been desiring for creating default Spring Test Context files. Your post has promoted me to open a Feature Request for auto-generating the spring text context. I'd recommend you either add a comment to that request, or open a separate feature request.

Ibatis2 and test context

I'm having a stupid configuration issue with Ibatis in my Spring project. Please don't jump on me about how all this was setup, I'm just following the "in house project structure policy".
So here is the structure, we have the "src/main/resources/META-INF/" folder that contains all of our config files used by the application, and then there is a "src/test/resources/META-INF/" that contains only the config files that have different settings to run unit testing.
Well in our case that's only one file, the src/main/resources/META-INF/spring/application-config.xml became the src/test/resources/META-INF/spring/test-application-config.xml. I'm am not going to outline the small differences between the two, because that part works fine.
The test-application-config.xml imports the src/main/resources/META-INF/spring/data-access-config.xml file just fine, which in turns use the src/main/resources/META-INF/ibatis/sqlmap-config.xml successfully... After that is when it goes to Hell.
See up until now we're using Spring to find the next config files in the classpath, but when we hit sqlmap-config.xml we leave the spring framework for the ibatis framework I believe, which loads the resource files defined inside it relative to the classpath (that's taken from the doc, whatever that means).
Inside the sqlmap-config.xml are defined a few resource files we're using that live inside the src/main/resources/META-INF/ibatis/mapping folder.
They are referenced like this:
<sqlMapConfig><sqlMap resource="/META-INF/ibatis/mapping/MyObject.xml"/></sqlMapConfig>
That works fine when I run the app normally, but when I run my JUnit test cases I get an IO exception stating that it can't find the file /META-INF/ibatis/mapping/MyObject.xml.
I've tried to change the path in the sqlmap-config.xml to "mapping/MyObject.xml" but that didn't help. I've also tried to use the Spring classpath prefix "classpath:META-INF/ibatis/mapping/MyObject.xml", didn't work either.
Anyone would have any idea on how to set that Ibatis properly so it works for both the app and the junit?
Thanks.
To solve this problem, I removed all the the Ibatis files and folders from the src/test/resources/META-INF folder.
The sqlmap-config.xml in src/main/resources/META-INF/ibatis/mapping file now maps like this:
<sqlMapConfig><sqlMap resource="META-INF/ibatis/mapping/MyObject.xml"/></sqlMapConfig>
Please note that compared to my initial post the leading "/" is gone... I think that's what made the difference here.
Hopes this helps anyone running into similar issues.
Just to see whether what you are saying is actually the problem.. you might want to place your mappings (MyObject.xml) in the same folder as sqlmap-config.xml. I say this because I've had my fair share of spring + ibatis + unit testing problems. (see resolved question asked by me)
Also, you might be getting IO exception because the mappings file does not exist outside the container (when you run tests).
You should also post definition for bean created from SqlMapClientFactoryBean. This should have configLocation property that contains path to sqlMapConfig xml
I had the same problem and could not find a (quick) solution that explained what exactly could be going wrong. Hence my answer.
As Spring documentation for Ibatis says:
Remember that iBATIS loads resources from the class path, so be sure
to add the 'Account.xml' file to the class path.
In your case by adding META-INF to your webproject build path i.e. if you used Eclipse, set <classpathentry kind="src" path="META-INF"/> in your projects' .classpath (This will be visible under Navigator view in Eclipse)

Resources