简体   繁体   English

如何正确使用 OneTimeSetUp 进行 NUnit 测试

[英]How to properly use OneTimeSetUp for NUnit Tests

I have a text file full of unsolved sudoku puzzles.我有一个充满未解决的数独谜题的文本文件。 I want to solve each puzzle with my algorithm and compare against a already working solver (from Nuget).我想用我的算法解决每个难题,并与已经工作的求解器(来自 Nuget)进行比较。 For this, I would like to use NUnit and furthermore create a OneTimeSetUp that adds each puzzle to a List, so I can iterate over it and solve it.为此,我想使用 NUnit 并进一步创建一个 OneTimeSetUp 将每个谜题添加到一个列表中,这样我就可以迭代它并解决它。 So far my approach looks like this:到目前为止,我的方法是这样的:

namespace SudokuSolverApp.SudokuSolverTests
{
    [SetUpFixture]
    public class Config
    {
        [OneTimeSetUp]
        public List<string> Init()
        {
            var sudokuPuzzles = new List<string>();
            var text = System.IO.File.ReadAllLines("/");
            for (var index = 0; index < text.Length; index++)
            {
                var puzzle = text[index];
                sudokuPuzzles.Add(puzzle);
            }

            return sudokuPuzzles;
        }
    }
    
    [TestFixture]
    public class SudokuUnitTests
    {
        public void TestAllSolutions()
        {
            foreach (var puzzle in sudokuPuzzles)
            {

                
            }
        }
    }
}

However, this does not work, because the program does not recognize the sudokuPuzzles variable in the last forloop.但是,这不起作用,因为程序不识别最后一个 forloop 中的sudokuPuzzles变量。 How would a properly written unit test look like in my case?在我的案例中,正确编写的单元测试会是什么样子?

As @funatparties points out, you are trying to access a non-existent variable in your test class. There are other problems as well, which I'll list正如@funatparties 指出的那样,您正试图在测试 class 中访问一个不存在的变量。还有其他问题,我将列出

  1. As stated, there's no variable SudokoUnitTests.sudokuPuzzles , so your program can't compile.如前所述,没有变量SudokoUnitTests.sudokuPuzzles ,因此您的程序无法编译。

  2. Even if there were such a variable, there's no reason to expect that NUnit would set it before calling your program.即使有这样一个变量,也没有理由期望 NUnit 会在调用您的程序之前设置它。 Read the NUnit docs!阅读 NUnit 文档!

  3. OneTimeSetUp methods don't return anything - they are void. OneTimeSetUp 方法不返回任何内容——它们是无效的。 Again: docs!再次:文档!

  4. SetUpFixtures are not designed to provide data to tests but to set up an environment to support the tests. SetUpFixtures 并非旨在为测试提供数据,而是用于设置支持测试的环境。 They are used for things like creating and populating a database or turning on some big piece of hardware you want to use in your tests.它们用于诸如创建和填充数据库或打开您要在测试中使用的一些大型硬件之类的事情。 (Maybe not so clear in the docs.) (也许在文档中不是很清楚。)

There are so many things in the above that should not even compile that it makes me wonder.上面有很多东西甚至不应该编译,这让我感到奇怪。 Are you using a real old version of NUnit, which doesn't even know about SetUpFixture?您使用的是真正的旧版 NUnit,它甚至不知道 SetUpFixture 吗? If that were the case, it would just be silently ignored.那样的话,只会默默无视。

Alternatives:备择方案:

  1. Use a OneTimeSetUp method in your test fixture rather than a SetUpFixture.在测试夹具中使用 OneTimeSetUp 方法而不是 SetUpFixture。 You can then save the list of puzzles as a member of the class and access it from each test.然后,您可以将拼图列表保存为 class 的成员,并从每个测试中访问它。

  2. If you only have one test method, which loops over each puzzle, t hen you may as well use a SetUp method rather than a OneTimeSetUp.如果您只有一种测试方法,它会循环遍历每个谜题,那么您也可以使用 SetUp 方法而不是 OneTimeSetUp。

  3. As a more advanced approach, you could have a separate test for each puzzle, taking the puzzle text as an argument.作为一种更高级的方法,您可以对每个谜题进行单独测试,将谜题文本作为参数。 That's what I would do.那就是我会做的。 But you need to know more about NUnit before doing that, so I suggest saving it for a later improvement.但是在这样做之前你需要了解更多关于 NUnit 的知识,所以我建议保存它以备后用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM