From 7799fcbbecb8ce857a27ab2a40872853f600aaa4 Mon Sep 17 00:00:00 2001 From: Andrei-Sav <68162973+Andrei-Sav@users.noreply.github.com> Date: Mon, 3 Jul 2023 13:57:46 +0300 Subject: [PATCH] Added the ability to run tests on a specific label (#8) * Added 'tmsLabelsOfTestsToRun' parameter and added description to Readme --------- Co-authored-by: andrey.savitsky Co-authored-by: Dmitry.Gridnev --- .github/workflows/release.yaml | 3 +- Tms.Adapter.Core/Tms.Adapter.Core.csproj | 2 +- .../Tms.Adapter.SpecFlowPlugin.csproj | 2 +- Tms.Adapter.XUnit/Tms.Adapter.XUnit.csproj | 2 +- Tms.Adapter/README.md | 34 ++++++++++-------- Tms.Adapter/Tms.Adapter.csproj | 2 +- TmsRunner/Models/Config.cs | 2 ++ TmsRunner/Options/AdapterConfig.cs | 6 +++- TmsRunner/Program.cs | 12 +++++-- TmsRunner/Services/FilterService.cs | 36 ++++++++++++++++++- TmsRunner/TmsRunner.csproj | 8 ++++- 11 files changed, 84 insertions(+), 25 deletions(-) diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index c4c0f04..bb739db 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -17,7 +17,8 @@ jobs: project: [ ./Tms.Adapter, ./Tms.Adapter.Core, - ./Tms.Adapter.XUnit + ./Tms.Adapter.XUnit, + ./Tms.Adapter.SpecFlowPlugin ] steps: - name: Checkout diff --git a/Tms.Adapter.Core/Tms.Adapter.Core.csproj b/Tms.Adapter.Core/Tms.Adapter.Core.csproj index f6c2060..5b9b21f 100644 --- a/Tms.Adapter.Core/Tms.Adapter.Core.csproj +++ b/Tms.Adapter.Core/Tms.Adapter.Core.csproj @@ -1,7 +1,7 @@ - 1.0.4 + 1.0.5 netstandard2.1 enable true diff --git a/Tms.Adapter.SpecFlowPlugin/Tms.Adapter.SpecFlowPlugin.csproj b/Tms.Adapter.SpecFlowPlugin/Tms.Adapter.SpecFlowPlugin.csproj index f969724..033a753 100644 --- a/Tms.Adapter.SpecFlowPlugin/Tms.Adapter.SpecFlowPlugin.csproj +++ b/Tms.Adapter.SpecFlowPlugin/Tms.Adapter.SpecFlowPlugin.csproj @@ -1,7 +1,7 @@ - 1.0.4 + 1.0.5 netstandard2.1 enable true diff --git a/Tms.Adapter.XUnit/Tms.Adapter.XUnit.csproj b/Tms.Adapter.XUnit/Tms.Adapter.XUnit.csproj index af9ba8b..7b9486c 100644 --- a/Tms.Adapter.XUnit/Tms.Adapter.XUnit.csproj +++ b/Tms.Adapter.XUnit/Tms.Adapter.XUnit.csproj @@ -1,7 +1,7 @@ - 1.0.4 + 1.0.5 netstandard2.1 enable true diff --git a/Tms.Adapter/README.md b/Tms.Adapter/README.md index b67dfd9..e8d2a02 100644 --- a/Tms.Adapter/README.md +++ b/Tms.Adapter/README.md @@ -27,7 +27,6 @@ dotnet package add TestIt.Adapter ### Configuration - | Description | Property | Environment variable | CLI argument | |----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------|-----------------------------------|-------------------------------| | Location of the TMS instance | url | TMS_URL | tmsUrl | @@ -39,6 +38,7 @@ dotnet package add TestIt.Adapter | Adapter mode. Default value - 0. The adapter supports following modes:
0 - in this mode, the adapter filters tests by test run ID and configuration ID, and sends the results to the test run
1 - in this mode, the adapter sends all results to the test run without filtering
2 - in this mode, the adapter creates a new test run and sends results to the new test run | adapterMode | TMS_ADAPTER_MODE | tmsAdapterMode | | It enables/disables certificate validation (**It's optional**). Default value - true | certValidation | TMS_CERT_VALIDATION | tmsCertValidation | | Mode of automatic creation test cases (**It's optional**). Default value - false. The adapter supports following modes:
true - in this mode, the adapter will create a test case linked to the created autotest (not to the updated autotest)
false - in this mode, the adapter will not create a test case | automaticCreationTestCases | TMS_AUTOMATIC_CREATION_TEST_CASES | tmsAutomaticCreationTestCases | +| List of labels for filtering tests (**Optional**). It will only work with adapter mode 2. | - | - | tmsLabelsOfTestsToRun | #### File @@ -46,15 +46,15 @@ Create **Tms.config.json** file in the project directory: ```json { - "url": "URL", - "privateToken": "USER_PRIVATE_TOKEN", - "projectId": "PROJECT_ID", - "configurationId": "CONFIGURATION_ID", - "testRunId": "TEST_RUN_ID", - "testRunName": "TEST_RUN_NAME", - "adapterMode": ADAPTER_MODE, - "automaticCreationTestCases": AUTOMATIC_CREATION_TEST_CASES, - "certValidation": CERT_VALIDATION + "url": "URL", + "privateToken": "USER_PRIVATE_TOKEN", + "projectId": "PROJECT_ID", + "configurationId": "CONFIGURATION_ID", + "testRunId": "TEST_RUN_ID", + "testRunName": "TEST_RUN_NAME", + "adapterMode": ADAPTER_MODE, + "automaticCreationTestCases": AUTOMATIC_CREATION_TEST_CASES, + "certValidation": CERT_VALIDATION } ``` @@ -64,13 +64,13 @@ Create **Tms.config.json** file in the project directory: ``` TmsRunner --runner "/usr/local/share/dotnet/sdk/6.0.302/vstest.console.dll" --testassembly "/tests/MsTest.dll" -tmsUrl=http://localhost:8080 -tmsPrivateToken=Token -tmsProjectId=f5da5bab-380a-4382-b36f-600083fdd795 -tmsConfigurationId=3a14fa45-b54e-4859-9998-cc502d4cc8c6 --tmsAdapterMode=0 -DtmsTestRunId=a17269da-bc65-4671-90dd-d3e3da92af80 -tmsTestRunName=Regress -tmsAutomaticCreationTestCases=true -tmsCertValidation=true --debug +-tmsAdapterMode=0 -DtmsTestRunId=a17269da-bc65-4671-90dd-d3e3da92af80 -tmsTestRunName=Regress -tmsAutomaticCreationTestCases=true -tmsCertValidation=true -tmsLabelsOfTestsToRun smoke,regress --debug ``` * `runner` - path to vstest.console.dll or vstest.console.exe * `testassembly` - path to dll with tests * `debug` - enable debug logs - + ### Attributes Use attributes to specify information about autotest. @@ -181,11 +181,15 @@ public class ParameterizedTests { You can help to develop the project. Any contributions are **greatly appreciated**. -* If you have suggestions for adding or removing projects, feel free to [open an issue](https://github.com/testit-tms/adapters-dotnet/issues/new) to discuss it, or create a direct pull request after you edit the *README.md* file with necessary changes. +* If you have suggestions for adding or removing projects, feel free + to [open an issue](https://github.com/testit-tms/adapters-dotnet/issues/new) to discuss it, or create a direct pull + request after you edit the *README.md* file with necessary changes. * Make sure to check your spelling and grammar. * Create individual PR for each suggestion. -* Read the [Code Of Conduct](https://github.com/testit-tms/adapters-dotnet/blob/main/CODE_OF_CONDUCT.md) before posting your first idea as well. +* Read the [Code Of Conduct](https://github.com/testit-tms/adapters-dotnet/blob/main/CODE_OF_CONDUCT.md) before posting + your first idea as well. # License -Distributed under the Apache-2.0 License. See [LICENSE](https://github.com/testit-tms/adapters-dotnet/blob/main/LICENSE.md) for more information. +Distributed under the Apache-2.0 License. +See [LICENSE](https://github.com/testit-tms/adapters-dotnet/blob/main/LICENSE.md) for more information. diff --git a/Tms.Adapter/Tms.Adapter.csproj b/Tms.Adapter/Tms.Adapter.csproj index f6322c5..61f7269 100644 --- a/Tms.Adapter/Tms.Adapter.csproj +++ b/Tms.Adapter/Tms.Adapter.csproj @@ -1,7 +1,7 @@ - 1.0.4 + 1.0.5 netstandard2.1 enable true diff --git a/TmsRunner/Models/Config.cs b/TmsRunner/Models/Config.cs index b77c1d2..6ea55a3 100644 --- a/TmsRunner/Models/Config.cs +++ b/TmsRunner/Models/Config.cs @@ -23,4 +23,6 @@ public class Config public string TmsAutomaticCreationTestCases { get; set; } public string TmsCertValidation { get; set; } + + public string TmsLabelsOfTestsToRun { get; set; } } \ No newline at end of file diff --git a/TmsRunner/Options/AdapterConfig.cs b/TmsRunner/Options/AdapterConfig.cs index 5f5749e..c9c63bc 100644 --- a/TmsRunner/Options/AdapterConfig.cs +++ b/TmsRunner/Options/AdapterConfig.cs @@ -22,6 +22,9 @@ public class AdapterConfig HelpText = "Set path to logger. Example: --logger '/Tests/logger.dll'")] public string LoggerPath { get; set; } + [Option("tmsLabelsOfTestsToRun", Required = false, HelpText = "Set labels of autotests to run. Example: --tmsLabelsOfTestsToRun smoke OR --tmsLabelsOfTestsToRun smoke,prod,cloud")] + public string TmsLabelsOfTestsToRun { get; set; } + [Option('d', "debug", Required = false, HelpText = "Set debug level for logging. Example: --debug")] public bool IsDebug { get; set; } @@ -73,7 +76,8 @@ public Config ToInternalConfig() TmsConfigFile = TmsConfigFile, TmsRunSettings = TmsRunSettings, TmsAutomaticCreationTestCases = TmsAutomaticCreationTestCases, - TmsCertValidation = TmsCertValidation + TmsCertValidation = TmsCertValidation, + TmsLabelsOfTestsToRun = TmsLabelsOfTestsToRun }; } } \ No newline at end of file diff --git a/TmsRunner/Program.cs b/TmsRunner/Program.cs index 399b6b6..0644cd9 100644 --- a/TmsRunner/Program.cs +++ b/TmsRunner/Program.cs @@ -43,19 +43,25 @@ public static async Task Main(string[] args) ITmsClient apiClient = new TmsClient(settings); var replacer = new Replacer(); + var filterService = new FilterService(replacer); switch (settings.AdapterMode) { case 0: { var testCaseForRun = await apiClient.GetAutoTestsForRun(settings.TestRunId); - var filterService = new FilterService(replacer); testCases = filterService.FilterTestCases(config.TestAssemblyPath, testCaseForRun, testCases); break; } case 2: { settings.TestRunId = await apiClient.CreateTestRun(); + + if (!string.IsNullOrEmpty(config.TmsLabelsOfTestsToRun)) + { + testCases = filterService.FilterTestCasesByLabels(config, testCases); + } + break; } } @@ -112,9 +118,11 @@ private static AdapterConfig GetAdapterConfiguration(IEnumerable args) TmsTestRunId = ac.TmsTestRunId, TmsTestRunName = ac.TmsTestRunName, TmsAdapterMode = ac.TmsAdapterMode, - TmsConfigFile = ac.TmsConfigFile + TmsConfigFile = ac.TmsConfigFile, + TmsLabelsOfTestsToRun = ac.TmsLabelsOfTestsToRun }; }); + return config; } } \ No newline at end of file diff --git a/TmsRunner/Services/FilterService.cs b/TmsRunner/Services/FilterService.cs index a69e67e..561ba48 100644 --- a/TmsRunner/Services/FilterService.cs +++ b/TmsRunner/Services/FilterService.cs @@ -4,6 +4,7 @@ using Tms.Adapter.Attributes; using Tms.Adapter.Utils; using TmsRunner.Extensions; +using TmsRunner.Options; namespace TmsRunner.Services; @@ -69,4 +70,37 @@ public List FilterTestCases( return testCasesToRun; } -} \ No newline at end of file + + public List FilterTestCasesByLabels( + AdapterConfig config, + IEnumerable testCases) + { + var labelsToRun = config.TmsLabelsOfTestsToRun.Split(',').Select(x => x.Trim()).ToList(); + var testCasesName = testCases.Select(t => t.FullyQualifiedName); + var testCasesToRun = new List(); + var assembly = Assembly.LoadFrom(config.TestAssemblyPath); + var testMethods = new List( + assembly.GetExportedTypes() + .SelectMany(type => type.GetMethods()) + .Where(m => testCasesName.Contains(m.DeclaringType!.FullName + "." + m.Name)) + ); + + foreach (var testMethod in testMethods) + { + var fullName = testMethod.DeclaringType!.FullName + "." + testMethod.Name; + + foreach (var attribute in testMethod.GetCustomAttributes(false)) + { + if (attribute is LabelsAttribute labelsAttr) + { + if (labelsAttr.Value.Any(labelsToRun.Contains)) + { + testCasesToRun.Add(testCases.FirstOrDefault(x => x.FullyQualifiedName == fullName)); + } + } + } + } + + return testCasesToRun; + } +} diff --git a/TmsRunner/TmsRunner.csproj b/TmsRunner/TmsRunner.csproj index e71b891..f25815d 100644 --- a/TmsRunner/TmsRunner.csproj +++ b/TmsRunner/TmsRunner.csproj @@ -1,7 +1,7 @@  - 1.0.4 + 1.0.5 Exe enable enable @@ -49,4 +49,10 @@ + + + Always + + +