如何忽略基于 NUnit 中另一个测试的测试? [英] How do I ignore a test based on another test in NUnit?

查看:35
本文介绍了如何忽略基于 NUnit 中另一个测试的测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为数据库操作编写一些 NUnit 测试.显然,如果 Add() 失败,那么 Get() 也会失败.然而,当 Add()Get() 都失败时,它看起来是骗人的,因为看起来有两个问题而不是一个.

I'm writing some NUnit tests for database operations. Obviously, if Add() fails, then Get() will fail as well. However, it looks deceiving when both Add() and Get() fail because it looks like there's two problems instead of just one.

有没有办法指定测试运行的顺序",如果第一个测试失败,后面的测试将被忽略?

Is there a way to specify an 'order' for tests to run in, in that if the first test fails, the following tests are ignored?

在同一行中,有没有办法对单元测试类本身进行排序?例如,我想先运行基本数据库操作的测试,然后再运行 UI 往返数据的测试.

In the same line, is there a way to order the unit test classes themselves? For example, I would like to run my tests for basic database operations first before the tests for round-tripping data from the UI.

注意:这与让测试相互依赖有点不同,它更像是在运行一堆测试之前确保某些东西首先起作用.例如,如果您一开始无法连接到数据库,那么运行一堆数据库操作就是浪费时间.

Note: This is a little different than having tests depend on each other, it's more like ensuring that something works first before running a bunch of tests. It's a waste of time to, for example, run a bunch of database operations if you can't get a connection to the database in the first place.

似乎有些人没有抓住重点.我不是这样做的:

It seems that some people are missing the point. I'm not doing this:

[Test]
public void AddTest()
{
    db.Add(someData);
}

[Test]
public void GetTest()
{
    db.Get(someData);
    Assert.That(data was retrieved successfully);
}

相反,我正在这样做:

[Test]
public void AddTest()
{
    db.Add(someData);
}

[Test]
public void GetTest()
{
    // need some way here to ensure that db.Add() can actually be performed successfully
    db.Add(someData);
    db.Get(somedata);
    Assert.That(data was retrieved successfully);
}

也就是说,我想先确保数据可以添加,然后才能测试是否可以检索.人们假设我使用第一个测试中的数据来通过第二个测试,但事实并非如此.我试图确保一个操作可能,然后再尝试另一个依赖它的操作.

In other words, I want to ensure that the data can be added in the first place before I can test whether it can be retrieved. People are assuming I'm using data from the first test to pass the second test when this is not the case. I'm trying to ensure that one operation is possible before attempting another that depends on it.

正如我已经说过的,在运行数据库操作之前,您需要确保可以连接到数据库.或者您可以在执行文件操作之前打开文件.或者在测试 API 调用之前连接到服务器.或者……你明白了.

As I said already, you need to ensure you can get a connection to the database before running database operations. Or that you can open a file before performing file operations. Or connect to a server before testing API calls. Or...you get the point.

推荐答案

NUnit 支持用于验证设置的Assume.That"语法.这被记录为 理论(感谢 克莱尔斯特雷布).在 NUnit.Framework 命名空间中是一个 Assume 类.引用文档:

NUnit supports an "Assume.That" syntax for validating setup.This is documented as part of the Theory (thanks clairestreb). In the NUnit.Framework namespace is a class Assume. To quote the documentation:

/// Provides static methods to express the assumptions
/// that must be met for a test to give a meaningful
/// result. If an assumption is not met, the test
/// should produce an inconclusive result.

所以在上下文中:

public void TestGet() {
    MyList sut = new MyList()
    Object expecting = new Object();
    sut.Put(expecting);
    Assume.That(sut.size(), Is(1));
    Assert.That(sut.Get(), Is(expecting));
}

这篇关于如何忽略基于 NUnit 中另一个测试的测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆