ricaun-io / RevitTest

Samples for the ricaun.RevitTest framework.
MIT License
21 stars 1 forks source link

Can you add support for "TestCaseSource"? #13

Closed FabianOswald-WolfSystem closed 1 month ago

FabianOswald-WolfSystem commented 2 months ago

Support TestCaseSourceAttribute to run test cases with a custom source.

NUnit API Reference

Sample

using NUnit.Framework;
using System.Collections.Generic;

public class TestsCaseSource
{
    public static int[] CasesSource = new[] { 1, 2, 3 };
    [TestCaseSource(nameof(CasesSource))]
    public void CasesSourceTest(int i)
    {
        Assert.True(i > 0);
    }

    static IEnumerable<int> CasesSourceMethod()
    {
        yield return 1;
        yield return 2;
        yield return 3;
    }
    [TestCaseSource(nameof(CasesSourceMethod))]
    public void CasesSourceMethodTest(int i)
    {
        Assert.True(i > 0);
    }

    static IEnumerable<int> CasesSourceMethodWithParameters(int start, int count)
    {
        for (int i = 0; i < count; i++)
        {
            yield return start + i;
        }
    }

    [TestCaseSource(nameof(CasesSourceMethodWithParameters), new object[] { 1, 4 })]
    public void CasesSourceMethodWithParametersTest(int i)
    {
        Assert.True(i > 0);
    }

    [TestCaseSource(typeof(AnotherClass), nameof(AnotherClass.CasesSource))]
    public void CasesSourceAnotherClassTest(int i, int j, int k)
    {
        Assert.True(i > 0);
        Assert.True(j > 0);
        Assert.True(k > 0);
    }
    public class AnotherClass
    {
        public static object[] CasesSource =
        {
            new object[] { 1, 2, 3 },
            new object[] { 2, 3, 4 },
            new object[] { 3, 4, 5 }
        };
    }
}

Edited by @ricaun

ricaun commented 2 months ago

What TestCaseSource means? Please add some description with a sample.

FabianOswald-WolfSystem commented 2 months ago

Of course, this would be a sample test:

public static IEnumerable<string> Names = new string[] { "Foo1", "Foo2", "Foo3" };

        [TestCaseSource(nameof(Names))]
        public void Foo(string name)
        {
            Console.WriteLine(name);
            Assert.True(true);
        }

https://docs.nunit.org/articles/nunit/writing-tests/attributes/testcasesource.html

ricaun commented 2 months ago

Interesting, is basically a compact TestCase with multiple source.

Never used this TestCaseSource and looks great, lets add support for that.

FabianOswald-WolfSystem commented 2 months ago

That would be really helpful. We run a test for all our Revit families, this takes about an hour. During this time I do not see any console output, as this is intercepted by NUnit and then output in the test result. In addition, the test is currently aborted after 10 minutes, I have not yet found a reason for this. With TestCaseSource I can run a separate test for each family.

ricaun commented 2 months ago

What kinda of test your are doing in the family, open and validating some parameters?

Actually I have a custom version of the NUnit to run the tests inside Revit, that's the reason the TestCaseSource does not work by default, and I pretty sure the console output is show in the test result, for me is the best feature 😀

The 10 minutes is hard coded, need to expose a option for that.

FabianOswald-WolfSystem commented 2 months ago

We need to support Revit families for multiple versions of Revit. We open the families and then save them again after they have been updated by Revit. This allows the end user to use the family without the update process. Parameters are also filled in. In future, the families may be translated into several languages. I also find it useful that the console output is summarized, especially when tests are executed in parallel, because this is the only way to assign which console output refers to which test. Unfortunately, in NUnit 3.13.3 there is no way to avoid this.

ricaun commented 2 months ago

We need to support Revit families for multiple versions of Revit. We open the families and then save them again after they have been updated by Revit. This allows the end user to use the family without the update process. Parameters are also filled in. In future, the families may be translated into several languages.

Nice, I kinda need to do the same thing with some of my families just to remove the update process in Revit to make the plugin more smooth.

Are you update all the families inside a folder or using the TestCase for selecting the family file name?

FabianOswald-WolfSystem commented 2 months ago

We have all families and test cases in one repository. All families are in one folder and are all updated together in one test. However, as soon as TestCaseSource is implemented, I will use one TestCase per family.

ricaun commented 2 months ago

We have all families and test cases in one repository. All families are in one folder and are all updated together in one test. However, as soon as TestCaseSource is implemented, I will use one TestCase per family.

Could you explain how you gonna do that using TestCaseSource? I suppose TestCaseSource gonna select each file name in a folder on the fly. If you could share a sample would be really useful.

FabianOswald-WolfSystem commented 2 months ago
        public static string[] GetFileNames()
        {
            return Directory.GetFiles(@"C:\Users\fabian.oswald\Downloads");
        }

        [TestCaseSource(nameof(GetFileNames))]
        public void TestFile(string fileName)
        {
            Console.WriteLine(fileName);
            Assert.True(true);
        }
ricaun commented 1 month ago

@FabianOswald-WolfSystem I prerelease a version to support TestCaseSource.

Should support the basic stuff, here is a sample. If you could give a quick test and feedback I appreciated.

using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace RevitTest.Sample
{
    public class Tests_Files
    {
        public static IEnumerable<string> GetFileNames()
        {
            var folder = Directory.GetCurrentDirectory();
            return Directory.GetFiles(folder, "*RevitTest*.dll").Select(Path.GetFileName);
        }

        [TestCaseSource(nameof(GetFileNames))]
        public void TestFile(string fileName)
        {
            Console.WriteLine(fileName);
            Assert.True(true);
        }

        public static string[] GetRevitFileNames()
        {
            var downloadFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Downloads");
            return Directory.GetFiles(downloadFolder, "*.rvt");
        }

        [TestCaseSource(nameof(GetRevitFileNames))]
        public void TestRevitFile(string fileName)
        {
            Console.WriteLine(fileName);
            Assert.True(true);
        }
    }
}
FabianOswald-WolfSystem commented 1 month ago

Thank you very much for the quick implementation. I have tested it and it works perfectly.

ricaun commented 1 month ago

Now the version 1.5.0 have support for the TestCaseSource