[英]ClassCleanup in MSTest is static, but the build server uses nunit to run the unit tests. How can i adjust?
MSTest has a [ClassCleanup()] attribute, which needs to be static as far as I can tell. MSTest具有[ClassCleanup()]属性,据我所知,该属性必须是静态的。 I like to run through after my unit tests have run,and clean up my database. 我喜欢在单元测试运行后进行操作,并清理数据库。 This all works great, however when I go to our build server and use our Nant build script, it seems like the unit tests are run with NUnit. 这一切都很好,但是当我转到构建服务器并使用Nant构建脚本时,似乎单元测试是使用NUnit运行的。 NUnit doesn't seem to like the cleanup method to be static. NUnit似乎不喜欢cleanup方法是静态的。 It therefore ignores my tests in that class. 因此,它忽略了我在该课程中的测试。 What can I do to remedy this? 我该怎么办才能补救? I prefer to not use [TestCleanUp()] as that is run after each test. 我不想使用[TestCleanUp()],因为它是在每次测试后运行的。 Does anyone have any suggestions? 有没有人有什么建议? I know [TestCleanup()] aids in decoupling, but I really prefer the [ClassCleanup()] in this situation. 我知道[TestCleanup()]有助于解耦,但在这种情况下,我真的更喜欢[ClassCleanup()]。 Here is some example code. 这是一些示例代码。
////Use ClassCleanup to run code after all tests have run
[ClassCleanup()]
public static void MyFacadeTestCleanup()
{
UpdateCleanup();
}
private static void UpdateCleanup()
{
DbCommand dbCommand;
Database db;
try
{
db = DatabaseFactory.CreateDatabase(TestConstants.DB_NAME);
int rowsAffected;
dbCommand = db.GetSqlStringCommand("DELETE FROM tblA WHERE biID=@biID");
db.AddInParameter(dbCommand, "biID", DbType.Int64, biToDelete);
rowsAffected = db.ExecuteNonQuery(dbCommand);
Debug.WriteLineIf(rowsAffected == TestConstants.ONE_ROW, string.Format("biId '{0}' was successfully deleted.", biToDelete));
} catch (SqlException ex) {
} finally {
dbCommand = null;
db = null;
biDelete = 0;
}
}
Thanks for any pointers and yes i realize I'm not catching anything. 感谢您的指导,是的,我意识到我什么都没捉到。 I need to get passed this hurdle first. 我需要首先克服这个障碍。
Cheers, ~ck in San Diego 干杯,〜ck在圣地亚哥
Your build server is ignoring your tests because MSTest uses a different set of attributes to specify tests to what NUnit uses. 您的构建服务器将忽略测试,因为MSTest使用一组不同的属性来指定NUnit使用的测试。 This is more likely the issue you're having, if it's not seeing any of your tests. 如果未看到任何测试,则很可能是您遇到的问题。
For example: MSTest uses [TestClass]
and [TestMethod]
to specify a test fixtures and tests, while NUnit uses [TestFixture]
and [Test]
. 例如:MSTest使用[TestClass]
和[TestMethod]
指定测试夹具和测试,而NUnit使用[TestFixture]
和[Test]
。
Also, NUnit's equivalent to [ClassCleanup]
is [TestFixtureTearDown]
, and it isn't static. 同样,NUnit等效于[ClassCleanup]
是[TestFixtureTearDown]
,它不是静态的。
Bear in mind that if your tests absolutely have to run on MSTest and NUnit, you can decorate the tests with attributes for both frameworks and it will work (to a certain degree, anyway). 请记住,如果您的测试绝对必须在MSTest和NUnit上运行,则可以使用这两个框架的属性来装饰测试,并且它将起作用(无论如何在一定程度上)。 However, to get ClassCleanup behaviour with both, you'd need something like this: 但是,要同时实现ClassCleanup行为,您需要这样的东西:
[ClassCleanup]
public static void MSTestClassCleanup()
{
CommonCleanup();
}
[TestFixtureTearDown]
public void NUnitTearDown()
{
CommonCleanup();
}
public static void CommonCleanup()
{
// Actually clean up here
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.