Unit Testing - Is it bad form to have unit test calling other unit tests

后端 未结 8 1933
遥遥无期
遥遥无期 2020-12-10 23:56

I have a unit test called TestMakeAValidCall(). It tests my phone app making a valid call.

I am about to write another test called TestShowCallMe

8条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-11 01:00

    "Could someone ellaborate on how the refactoring would look like in this case? – Philip Bergström Nov 28 '15 at 15:33"

    I am currently doing something like this and this is what i came up with:

    Notice that ProcessorType and BuildProcessors both call TestLevels

    the actual content besides that fact is unimportant

    its using XUnit, and Shouldly NuGet package

    private static void TestLevels(ArgProcessor incomingProcessor)
    {
        Action currentLevelIteration = null;
        currentLevelIteration = (currentProcessor, currentLevel) =>
        {
            currentProcessor.CurrentLevel.ShouldBeEquivalentTo(currentLevel);
            ProcessorLevel nextProcessor = currentProcessor.CurrentProcessor;
            if (nextProcessor != null)
                currentLevelIteration(nextProcessor, currentLevel + 1);
        };
    
        currentLevelIteration(incomingProcessor, 0);
    }
    
    [Theory]
    [InlineData(typeof(Build), "Build")]
    public void ProcessorType(Type ProcessorType, params string[] args)
    {
        ArgProcessor newCLI = new OriWeb_CLI.ArgProcessor(args);
    
        IncomingArgumentsTests.TestLevels(newCLI);
    
        newCLI.CurrentProcessor.ShouldBeOfType(ProcessorType);
    }
    
    [Theory]
    [InlineData(typeof(Build.TypeScript), "TypeScript")]
    [InlineData(typeof(Build.CSharp), "CSharp")]
    public void BuildProcessors(Type ProcessorType, params string[] args)
    {
        List newArgs = new List {"Build"};
        foreach(string arg in args) newArgs.Add(arg);
    
        ArgProcessor newCLI = new OriWeb_CLI.ArgProcessor(newArgs.ToArray());
    
        IncomingArgumentsTests.TestLevels(newCLI);
    
        newCLI.CurrentProcessor.CurrentProcessor.ShouldBeOfType(ProcessorType);
    }
    

提交回复
热议问题