在测试失败时运行adiitional cleanup功能(当Assertion失败时)

时间:2016-01-01 00:17:18

标签: c# unit-testing selenium nunit assert

我目前有一个测试套件使用Nunit和C#与Selenium WebDriver使用断言,有没有办法只在断言失败时运行一个函数。通常情况下,这将继续进行拆解,然后进行下一次测试,但是如果之前的测试失败,我需要重置测试中的某些参数。这样做的最佳方式是什么?

1 个答案:

答案 0 :(得分:2)

您可以扩展TestActionAttribute以处理此问题。在function getStuffCached( $index ) { static $cache = array(); $postID = get_the_ID(); if( isset( $cache[ $postID ] ) === false ) { $cache[ $postID ] = getStuffFromDatabaseAsArray(); } return ( isset( $cache[ $postID ][ $index ] ) ) ? $cache[ $postID ][ $index ] : null; } 覆盖中,您可以检查AfterTest()并进行相应处理。如,

TestContext.CurrentContext.Result.Status == TestStatus.Failed