0

I want to call many tests like this.

        var test8001 = new Test8001();
        test8001.Execute(drv);
        var test8002 = new Test8002();
        test8002.Execute(drv);
        var test8007 = new Test8007();
        test8007.Execute(drv);

How can I automatically instantiate all test function with a int list of all test numbers?

        List<int> classNameNumbers = new List<int>() { 8001, 8002, 8007 };

I need a for-loop where Execute() is called on every instance.

Edit: The name of the type e.g. 'Test8001' should be retrieved from my integer list.

11
  • Build a simple console application that generates the code in a txt file, then copy/paste that generated code into your actual application. 9 times out of 10, if you're a relatively competent developer, it's easier to write a text generation tool yourself, compared to learning to use a tool someone else has created (unless you want to generate really complex code, which this example is not) Commented Oct 2, 2017 at 16:14
  • Note that I could have also suggested reflection, but I think you're not really interested in generating the tests at runtime, you're just looking for a way to generate code before compile time. If I misunderstood that, my earlier comment does not apply and reflection would be the way to go. Commented Oct 2, 2017 at 16:15
  • 1
    You're passing a parameter to only one of those tests? Commented Oct 2, 2017 at 16:15
  • If these are actually unit tests, consider using an actual test framework. Commented Oct 2, 2017 at 16:15
  • 1
    Reflection will work but it won't be pretty. If a class doesn't have the right name or the method doesn't have the right signature it will throw an exception or fail silently. That's something we don't want in production code, but we have to maintain our tests just like we have to maintain production code. TBH I'd take it as a reason to rethink why the tests need to be run this way. Commented Oct 2, 2017 at 16:20

1 Answer 1

2

Try out the following

namespace Stackoverflow46529447
{
    class Program
    {
        static void Main(string[] args)
        {
            var drv = new Drv();

            var numbers = new[] {8001, 8002, 8003};
            var executables = numbers.Select(x => Activator.CreateInstance(Type.GetType($"Stackoverflow46529447.Test{x:0000}")))
                .OfType<IExecutable>()
                .ToArray();

            foreach (var executable in executables)
            {
                executable.Execute(drv);
            }
        }
    }

    public class Test8001 : IExecutable
    {
        public void Execute(Drv drv)
        {
            Console.WriteLine("Hello from Test 8001");
        }
    }

    public class Test8002 : IExecutable
    {
        public void Execute(Drv drv)
        {
            Console.WriteLine("Hello from Test 8002");
        }
    }

    public class Test8003 : IExecutable
    {
        public void Execute(Drv drv)
        {
            Console.WriteLine("Hello from Test 8003");
        }
    }

    public interface IExecutable
    {
        void Execute(Drv drv);
    }

    public class Drv
    {

    }
}

This uses reflection to create instance types.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.