Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 953617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:04:50+00:00 2026-05-16T00:04:50+00:00

Is it possible to get the values for a TestCaseAttribute from an external data

  • 0

Is it possible to get the values for a TestCaseAttribute from an external data source such as an Excel Spreadsheet, CSV file or Database? i.e. Have a .csv file with 1 row of data per test case and pass that data to NUnit one at a time.

Here’s the specific situation that I’d like to use this for. I’m currently merging some features from one system into another. This is pretty much just a copy and paste process from the old system into the new one. Unfortunately, the code being moved not only does not have any tests, but is not written in a testable manner (i.e. tightly coupled with the database and other code.) Taking the time to make the code testable isn’t really possible since its a big mess, i’m on a tight schedule and the entire feature is scheduled to be re-written from the ground up in the next 6-9 months. However, since I don’t like the idea of not having any tests around the code, I’m going to create some simple Selenium tests using WebDriver to test the page through the UI. While this is not ideal, it’s better than nothing.

The page in question has about 10 input values and about 20 values that I need to assert against after the calculations are completed, with about 30 valid combinations of values that I’d like to test. I already have the data in a spreadsheet so it’d be nice to simply be able to pull that out rather than having to re-type it all in Visual Studio.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T00:04:51+00:00Added an answer on May 16, 2026 at 12:04 am

    I was finally able to accomplish what I wanted using NUnit’s TestCaseSource attribute. The code looks a little ugly but it works.

    Here is an example of pullind the data from a .csv file and passing it to the test method. The test is for the Add method of a simple calculator that takes two ints, adds them together and returns the sum.

    Class to load the test data from the file.

    public class TestData
    {
        public int number1 { get; set; }
        public int number2 { get; set; }
        public int sum { get; set; }
    
        public static IEnumerable TestCases
        {
            get
            {
                string inputLine;
                using(FileStream inputStream = 
                    new FileStream("C:\\Code\\TestData\\TestData.csv", 
                        FileMode.Open, 
                        FileAccess.Read))
                {
                    StreamReader streamReader = new StreamReader(inputStream);
    
                    while((inputLine = streamReader.ReadLine()) != null)
                    {
                        var data = inputLine.Split(',');
                        yield return new TestData { 
                            number1 = Convert.ToInt32(data[0])
                            ,number2 = Convert.ToInt32(data[1])
                            ,sum = Convert.ToInt32(data[2]) 
                        };
                    }
    
                    streamReader.Close();
                    inputStream.Close();
                }
            }
        }
    }
    

    Class with the actual tests:

    [TestFixture]
    public class CalculatorTests
    {
        [Test]
        [TestCaseSource(typeof(TestData), "TestCases")]
        public void AddTwoNumbers(TestData data)
        {
            int sum = Calculator.Add(data.number1, data.number2);
            sum.ShouldEqual(data.sum);
        }
    }
    

    Contents of TestData.csv

    4,4,8
    15,20,35
    8,8,16
    5,5,10
    42,13,55
    

    It should be fairly simple to modify the get property in the TestData class to pull data from any datasource you want (i.e. Database, Web Service, Excel…)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 469k
  • Answers 469k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I don't know how IIS UrlRewrite works, but at a… May 16, 2026 at 2:34 am
  • Editorial Team
    Editorial Team added an answer Your issue has a resolution, but it will not be… May 16, 2026 at 2:34 am
  • Editorial Team
    Editorial Team added an answer Yes, you can. I do something similar at one of… May 16, 2026 at 2:34 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.