MSTest 中的 ClassCleanup 是静态的,但构建服务器使用 nunit 来运行单元测试。我该如何调整?
MSTest 有一个 [ClassCleanup()] 属性,据我所知,它需要是静态的。我喜欢在单元测试运行后运行,并清理数据库。这一切都很好,但是当我转到构建服务器并使用 Nant 构建脚本时,单元测试似乎是使用 NUnit 运行的。 NUnit 似乎不喜欢静态的清理方法。因此它忽略了我在那堂课上的测试。我可以做什么来补救这个问题?我宁愿不使用 [TestCleanUp()],因为它会在每次测试后运行。有人有什么建议吗?我知道 [TestCleanup()] 有助于解耦,但在这种情况下我真的更喜欢 [ClassCleanup()]。这是一些示例代码。
////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;
}
}
感谢您的任何指点,是的,我意识到我没有抓住任何东西。我得先跨过这个坎。
干杯, 〜ck在圣地亚哥
MSTest has a [ClassCleanup()] attribute, which needs to be static as far as I can tell. 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. NUnit doesn't seem to like the cleanup method to be static. 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. Does anyone have any suggestions? I know [TestCleanup()] aids in decoupling, but I really prefer the [ClassCleanup()] in this situation. 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的构建服务器会忽略您的测试,因为 MSTest 使用一组不同的属性来指定 NUnit 使用的测试。如果没有看到您的任何测试,这很可能是您遇到的问题。
例如:MSTest 使用
[TestClass]
和[TestMethod]
来指定测试装置和测试,而 NUnit 使用[TestFixture]
和[测试]
。此外,NUnit 等价于
[ClassCleanup]
的是[TestFixtureTearDown]
,而且它不是静态的。请记住,如果您的测试绝对必须在 MSTest 和 NUnit 上运行,您可以使用这两个框架的属性来装饰测试,并且它会起作用(无论如何,在某种程度上)。但是,要同时获得 ClassCleanup 行为,您需要如下所示:
Your build server is ignoring your tests because MSTest uses a different set of attributes to specify tests to what NUnit uses. 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]
.Also, NUnit's equivalent to
[ClassCleanup]
is[TestFixtureTearDown]
, and it isn't static.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). However, to get ClassCleanup behaviour with both, you'd need something like this: