Skip to content

Support DeploymentItem in .Net Core projects #96

Closed

Description

Description

[DeploymentItem] attribute is a no-op in the .Net Core world. This is however supported in Desktop and is required for functional testing scenarios and to provide isolation for test runs. Filing this work-item to add in that support for .Net Core based projects as well.

Steps to reproduce

  1. In a .Net Core project add a DeploymentItem on a class/method linking to an xml resource(say) needed for the run.
  2. Run a test that reads this xml resource.

Expected behavior

The test deploys the xml resource to a deployment directory and runs the test from there.

Actual behavior

The test fails complaining about not being able to find the resource file.

Activity

  1. orellabac commented on Feb 5, 2019

    @orellabac

    I am currently using this workaround for simple cases:

            // Declare this property - this is set by MSTest
            public TestContext TestContext { get; set; }
    
            // In test initialization - note the signature should be exactly this
            // A static void method with one argument of type TestContext 
            [TestInitialize]
            public void TestSetup()
            {
                var testName = TestContext.TestName;
                var method = new StackFrame().GetMethod().DeclaringType.GetMethod(testName);
                var attributes = method.GetCustomAttributes(typeof(DeploymentItemAttribute)).ToArray();
                foreach(DeploymentItemAttribute att in attributes)
                {
                    var origin = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), att.Path);
                    var targetDir = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), att.OutputDirectory);
                    // Creates dir for deployment item
                    Directory.CreateDirectory(targetDir);
                    // Copies item
                    try
                    {
                        File.Copy(origin, targetDir);
                    }
                    catch (Exception ex)
                    {
                        System.Diagnostics.Debug.WriteLine("Error copying deployment item {0} to {1} message {2}", origin, targetDir, ex.Message);
                    }
                }
            }
  2. jayaranigarg commented on Apr 10, 2019

    @jayaranigarg
    Member

    Fixed with #565

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions