Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# using static variable as parameter to DeploymentItem

I want to use a static variable as the parameter to DeploymentItem on an MSTest unit test but it doesn't seem I'm able to do so. There's an XSL file that needs to be copied along with the DLL file when the unit test runs, and I defined the location as

private static string _xslPath = Path.Combine("MyProjectDir", "transform.xsl");

However, when I then do the following:

[TestMethod]
[DeploymentItem(DLL)]
[DeploymentItem(_xslPath)]
public void XmlToResultsTest() { }

I get this build error:

An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type

Okay okay, fine, but it just seems so dirty to assemble the path myself:

[DeploymentItem(@"MyProjectDir\transform.xsl")]

Am I being overly picky here about wanting to use Path.Combine? Is there another alternative I'm missing? I suppose I could just put the XSL file in the root solution directory so I don't have to pass in the project directory as part of the path.

like image 355
Sarah Vessels Avatar asked Jan 25 '10 16:01

Sarah Vessels


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


2 Answers

Attributes can only use constant strings, so no: you can't do this (you would have to use the pre-combined version, or literal concatenation - not Path.Combine). You could use the test-project deployment settings too (testrunconfig?), but frankly I prefer to use the NUnit approach of just marking the file (in the csproj, like normal) for deployment. I have yet to figure out why MS added a separate way of defining this...

like image 190
Marc Gravell Avatar answered Sep 27 '22 20:09

Marc Gravell


This should work:

[TestClass]
[DeploymentItem(TestParams.ConfigFileName)]
public class MyTest
{
    private static class TestParams
    {
        public const string ConfigFileName = "TestConfig.xml";
    }
// ...
}
like image 27
ShloEmi Avatar answered Sep 27 '22 19:09

ShloEmi