- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
对于我们的应用程序,我们有大约 4000 个单元测试,如果我们将您的代码 checkin tfs,这些单元测试将自动执行。
我们在 Build-Definition 中做了很多更改,因此现在要求所有单元测试必须具有属性 [TestCategory(TestCategories.GatedCheckin)]
才能在 gated- 中执行签到。
不幸的是,4000 个单元测试中只有 700 个已经具有此属性。现在我必须将该属性添加到剩余的单元测试中。
为此,我编写了一个小型 VisualStudio-Extension,我可以在其中打开源文件并搜索以下正则表达式:
^([\t]|[ ])*\[TestMethod\]([\t]|[ ]|[\w\/äÄüÜöÖß])*([\r\n]+)([\t]|[ ])*public
这个正则表达式非常适用于像这样的单元测试:
[TestMethod]
public void PathIsValidTest1()
{...}
或
[TestMethod] // another test
public void Calculator_Add_3_And_3_Equals_6_Test()
{...}
但是对于还包含另一个属性的单元测试,例如:
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void ThrowOnInputTooLongTest2()
{...}
正则表达式不起作用。
我如何修改正则表达式,使其匹配所有具有 [TestMethod]
属性而不是 [TestCategory(TestCategories.GatedCheckin)]
的单元测试
我想过使用 ?!
进行负面前瞻,但我没有让它工作。
有什么想法吗?
我修改了 Addison 提供的解决方案,使其看起来像:
^[\t ]*\[TestMethod\][ \t\w\/äÄüÜöÖß]*(\n([ \t]*)\[(?!TestCategory\(TestCategories\.GatedCheckin\)).+\][ \t\w\/äÄüÜöÖß]*)?\s+public
如果我在 regex101 中使用它,它就可以正常工作,如您所见 here
但是如果我在 c# 中使用这个正则表达式:
string content = File.ReadAllText(file);
Regex regex = new Regex(pattern, RegexOptions.Multiline);
int matchCount = regex.Matches(content).Count;
我只得到 2 个匹配项。
最佳答案
好问题!
我设法做到了这一点:
^[\t ]*\[TestMethod\][ \t\w\/äÄüÜöÖß]*(\n\[(?!TestCategory\(TestCategories\.GatedCheckin\)).+\][ \t\w\/äÄüÜöÖß]*)*\s+public
我添加了另一个捕获字段(并简化了正则表达式的其余部分),以便它现在在第一个 [TestMethod]
之后检查任意数量的其他 []
, 当且仅当它们都不是 [TestCategory(TestCategories.GatedCheckin)]
时才会接受它们。
这里有一些 C# 代码可以做到这一点:
using System;
using System.Text.RegularExpressions;
namespace Programming {
class Program {
static void Main() {
string content = "namespace MyNameSpace1\n{\n [TestClass]\n public class GetMapPathActionTests\n {\n [TestMethod] // 1\n public void GetMapPath_with_All_Attibutes()\n {\n ...\n }\n [TestMethod] // 2\n [ExpectedException(typeof(DBInstallInfoConverterCrossCheckerRequiredChildNotFoundException))]\n public void GetMapPath_with_Empty_Input()\n {\n \n }\n [TestMethod] // 3\n [ExpectedException(typeof(DBInstallInfoConverterCrossCheckerRequiredChildNotFoundException))]\n public void GetMapPath_with_Empty_Output()\n {\n \n }\n [TestMethod] // 4\n public void GetMapPath_with_Empty()\n {\n \n }\n [TestMethod] // 5\n [ExpectedException(typeof(DBInstallInfoConverterCrossCheckerRequiredChildNotFoundException))]\n public void GetMapPath_with_All_Attibutes_Empty()\n {\n \n }\n }\n}\n";
Regex regex = new Regex(@"^[\t ]*\[TestMethod\][ \t\w\/äÄüÜöÖß]*(\s+\[(?!TestCategory\(TestCategories\.GatedCheckin\)).+\][ \t\w\/äÄüÜöÖß]*)?\s+public", RegexOptions.Multiline);
MatchCollection matches = regex.Matches(content);
foreach (Match match in matches) {
foreach (Capture capture in match.Captures) {
Console.WriteLine(capture.Value);
}
}
}
}
}
关于c# - 匹配 TestMethods 的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54205733/
我正在使用 Visual Studio 单元测试 fwk 为 C# 项目编写单元测试,发现使用 [TestMethod] 或 [TestMethod()] - 任何属性工作正常。它们有什么区别? 最佳
我有一个项目,在每个 @Test 方法之前我都会检查该方法的注释数据是否有效。如果数据无效,我想跳过测试方法并继续测试套件的其余部分。 所有数据解析和逻辑都工作正常,但据我所知,我使用了错误的工具来完
我已经能够自动化我的应用程序的整个流程。我有一个 testMethod() 来处理流程的自动化。我想将这个单一方法拆分为其他几个方法,以提高代码的可读性。但是,我在这里担心,如果我将测试方法拆分为其他
我有两个 [TestMethods]。 MethodA的结果需要作为MethodB的输入。问题是当一个新的测试方法开始时,所有的值和变量都会被重置。 已经有人问过 exact same questio
我在 WebAPI MVC 项目中有一个相当复杂的方法。它可以做很多事情,包括访问远程服务器进行用户身份验证。根据此结果,它返回一个重定向(页面)、一条字符串错误消息或一个表示所有身份验证猴子业务的对
对于我们的应用程序,我们有大约 4000 个单元测试,如果我们将您的代码 checkin tfs,这些单元测试将自动执行。 我们在 Build-Definition 中做了很多更改,因此现在要求所有单
这可能是我需要摆脱我的系统的哲学咆哮,另一方面我可能做错了什么,如果是这样请告诉我! 我有一个单元测试看起来像这样: [TestMethod] [ExpectedException(typeof(No
诚然,我是 VS2010 单元测试的新手,但我遇到了一个我似乎无法解决的问题。 无论我在测试项目中有多少类用 [TestClass] 修饰,或者这些测试类中有多少方法用 [TestMethod] 修饰
我有一个基于 .NET 4.6.1 (VS2017) 构建的 C# 应用程序。 我现在正在构建一个测试环境,并希望包括用户群测试。 用户提供一个文件文件夹,每个文件都是我需要运行的单独用户测试。 我想
我有 TestMethod,我需要在不同的 N 线程中运行它 N 次。我想这样做是为了测试我的 WebMethod 的行为——我可以在同一时刻从不同的线程获得多个请求。 如何在单元测试 C# 中的多个
好吧,愚蠢的问题。我正在尝试在 VS2005 中设置我的第一个 TypeMock 演示项目,但它无法识别 [TestMethod] 属性。我已经包含了 TypeMock 和 TypeMock.Arra
有没有办法知道何时通过运行测试方法调用代码? bool MyMethod() { if ( /* are we running a test? */ ) { retur
这个问题在这里已经有了答案: How to write to Console.Out during execution of an MSTest test (6 个答案) 关闭 9 年前。 我正在尝
今天,我将解决方案的所有基础项目从VS2008升级到VS2010。除了我的单元测试外,其他一切都进展顺利。 首先,只有Web项目将.NET 4作为目标框架。所有其他项目仍然具有.NET 3.5。我将它
这个问题涉及一种通用单元测试技术,该技术具有潜在的非常有用的广泛适用场景。但是用一个例子来更好地说明我的问题更容易理解。 假设我想测试所有覆盖 Equals() 的类型这样做是正确的。由于Equals
我正在使用 VS Unit 和 Moq 进行单元测试,但我无法找出放置模拟设置的正确位置。 目前我正在设置这样的模拟: [TestMethod] public void SomeTestMethod(
公共(public)类 MyBenchmark{ @Benchmark public void otherMethod() { } @Benchmark public void sessionCrea
我想在 [TestInitialize] 方法中初始化经常使用的变量,但我不希望为每个测试方法初始化相同的变量。有没有办法通过装饰器或类似的东西来区分测试方法?例如 [TestInitialize]
我正在使用自定义规则集,该规则集错误地打开了 CA1822。 我有一个 TestMethod,它是异步的,所以它返回一个任务。此方法不使用任何字段,因此代码 Analysis flags 规则 CA1
目前,我正在通过在每个 TestMethod 中创建被测对象来编写我的单元测试。这样做的主要原因是为了促进自包含、易于阅读/调试以及可能在构建期间调整依赖关系。 public class MyClas
我是一名优秀的程序员,十分优秀!