How can I test my C# Console App using XUnit, the App contain Command line arguments

Viewed 14

I have a Program, it's C# Console App, The function of the program is to open a file and search for a specific line and edit it.

I can run the program without Parameter and the local file will be edited, or i can run the program with a parameter to Specify the path of the file I want to modify, or i can run the program with a parameter to get help.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using static System.Net.Mime.MediaTypeNames;

namespace BuildIncrement
{
    public class Program
    {
        static void Main(string[] args)
        {

            Console.WriteLine(args.Length);
            string filePath = "version.h";

            if (args.Length > 0)
            {
                if (args[0].Contains("path"))
                {
                    var argsArray = args[0].Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
                    if (argsArray.Length > 1)
                    {
                        filePath = argsArray[1];
                    }
                }
                else if (args[0].Contains("help"))
                {
                    Console.WriteLine("To run the Program please type --path");
                    return;
                }
            }

            if (File.Exists(filePath) == false)
            {
                Console.WriteLine("Fehler: Die Datei existiert nicht!");
                return;
            }

            List<string> lines = File.ReadAllLines(filePath).ToList();

            string line = lines.Where(i => i.Contains("BUILD_NUM")).FirstOrDefault();

            //foreach (string line in lines)
            int buildNr = 0;

            if (line != null)
            {
                string[] lineparts = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                if (lineparts.Length > 2)
                {
                    string Nr = lineparts[2];
                    if (int.TryParse(Nr, out buildNr))
                    {

                    }
                    buildNr++;
                    int index = lines.IndexOf(line);
                    lines.RemoveAt(index);
                    lines.Insert(index, line.Replace(Nr, buildNr.ToString()));
                }
            }

            File.WriteAllLines(filePath, lines);

            Console.WriteLine($"Build-Nr wurde auf {buildNr} aktualisiert!");
        }

    }

}
2 Answers

Rectify your code and write the logic in different methods. Call these methods in Main. Then You can use Xunit [Fact] to test your code.

It is perfectly possible to call the Main method and supply arguments to it. (You do have to make it a public method).

        [Fact]
        public void MyTest()
        {
            string[] args = new string[] { "MyFile.txt" };
            Program.Main(args);
            // Assertions about the result
        }
Related