Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Directory while running unit test

Hi when running my unit test I'm wanting to get the directory my project is running in to retrieve a file.

Say I have a Test project named MyProject. Test I run:

AppDomain.CurrentDomain.SetupInformation.ApplicationBase 

and I receive "C:\\Source\\MyProject.Test\\bin\\Debug".

This is close to what I'm after. I don't want the bin\\Debug part.

Anyone know how instead I could get "C:\\Source\\MyProject.Test\\"?

like image 521
AnonyMouse Avatar asked Apr 18 '12 06:04

AnonyMouse


People also ask

How do I run a unit test file?

To run all the tests in a default group, choose the Run icon and then choose the group on the menu. Select the individual tests that you want to run, open the right-click menu for a selected test and then choose Run Selected Tests (or press Ctrl + R, T).

Should I run unit tests before build?

Always build the project(s) before running/debugging unit tests. Positive: always correct results, not confusing for users. in our case) even in case there were no changes at all.

Are unit tests run locally?

Unit tests should run on your local machine AND on the build server. They are an invaluable resource as feedback for the developer. The DEV should write the unit tests. Before checking in, he should run the unit tests to make sure he does not break anything.


2 Answers

I would do it differently.

I suggest making that file part of the solution/project. Then right-click -> Properties -> Copy To Output = Copy Always.

That file will then be copied to whatever your output directory is (e.g. C:\Source\MyProject.Test\bin\Debug).

Edit: Copy To Output = Copy if Newer is the better option

like image 196
Ilian Avatar answered Oct 14 '22 18:10

Ilian


Usually you retrieve your solution directory (or project directory, depending on your solution structure) like this:

string solution_dir = Path.GetDirectoryName( Path.GetDirectoryName(     TestContext.CurrentContext.TestDirectory ) ); 

This will give you the parent directory of the "TestResults" folder created by testing projects.

like image 28
Darien Pardinas Avatar answered Oct 14 '22 20:10

Darien Pardinas