locked
How can we use the file names in a directory as datasource for unit testing? RRS feed

  • Question

  • Hi ,

    I am using the unit test framework of visual studio. For a specific situation I want to use the file names available within a directory as data source.

    For example :  Suppose directory D1 Contains three files namely :  F1, F2 and F3.

    For a particular test I will assign the path of directory as data source and the test will be executed thrice for F1,F2 & F3.

    Kindly let me know If this sounds achievable.

    Any kind of help is appreciated.

    Thanks,

     

     

     

    Thursday, February 2, 2012 9:59 AM

Answers

  • Hello!

    One possible way to achieve this is to use one test that runs depending tests.

    Like this:

     

    // Subject:
    
        class Hokum
        {
            public string input;
    
            public Hokum(string input)
            {
                this.input = input;
            }
        }
    
    
    // Test:
            [TestMethod()]
            public void HokumConstructorTest()
            {
                // Arrange:
                List<string> SomeParams = new List<string>();
                SomeParams.Add("some");
                SomeParams.Add("params");
                SomeParams.Add("you get from somewhere");
    
                // Act:
                foreach (string param in SomeParams)
                {
                    HokumConstructorSubTest(param);
                }
            }
    
            public void HokumConstructorSubTest(string input)
            {
                Hokum testHokum = new Hokum(input);
                string expected = input;
                string actual = testHokum.input;<br/><br/>            // Assert:
                Assert.AreEqual(expected, actual);
            }
    

     

    Hope this helps,

     

    Max


    Thursday, February 2, 2012 2:42 PM