Skip to content

Commit

Permalink
Added the ability to run tests on a specific label (#8)
Browse files Browse the repository at this point in the history
* Added 'tmsLabelsOfTestsToRun' parameter and added description to Readme

---------

Co-authored-by: andrey.savitsky <[email protected]>
Co-authored-by: Dmitry.Gridnev <[email protected]>
  • Loading branch information
3 people authored Jul 3, 2023
1 parent f5aa24a commit 7799fcb
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 25 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ jobs:
project: [
./Tms.Adapter,
./Tms.Adapter.Core,
./Tms.Adapter.XUnit
./Tms.Adapter.XUnit,
./Tms.Adapter.SpecFlowPlugin
]
steps:
- name: Checkout
Expand Down
2 changes: 1 addition & 1 deletion Tms.Adapter.Core/Tms.Adapter.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>1.0.4</Version>
<Version>1.0.5</Version>
<TargetFramework>netstandard2.1</TargetFramework>
<Nullable>enable</Nullable>
<IsPackable>true</IsPackable>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>1.0.4</Version>
<Version>1.0.5</Version>
<TargetFramework>netstandard2.1</TargetFramework>
<Nullable>enable</Nullable>
<IsPackable>true</IsPackable>
Expand Down
2 changes: 1 addition & 1 deletion Tms.Adapter.XUnit/Tms.Adapter.XUnit.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>1.0.4</Version>
<Version>1.0.5</Version>
<TargetFramework>netstandard2.1</TargetFramework>
<Nullable>enable</Nullable>
<IsPackable>true</IsPackable>
Expand Down
34 changes: 19 additions & 15 deletions Tms.Adapter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand All @@ -39,22 +38,23 @@ dotnet package add TestIt.Adapter
| Adapter mode. Default value - 0. The adapter supports following modes:<br/>0 - in this mode, the adapter filters tests by test run ID and configuration ID, and sends the results to the test run<br/>1 - in this mode, the adapter sends all results to the test run without filtering<br/>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:<br/>true - in this mode, the adapter will create a test case linked to the created autotest (not to the updated autotest)<br/>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

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
}
```

Expand All @@ -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.
Expand Down Expand Up @@ -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.
2 changes: 1 addition & 1 deletion Tms.Adapter/Tms.Adapter.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>1.0.4</Version>
<Version>1.0.5</Version>
<TargetFramework>netstandard2.1</TargetFramework>
<Nullable>enable</Nullable>
<IsPackable>true</IsPackable>
Expand Down
2 changes: 2 additions & 0 deletions TmsRunner/Models/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ public class Config
public string TmsAutomaticCreationTestCases { get; set; }

public string TmsCertValidation { get; set; }

public string TmsLabelsOfTestsToRun { get; set; }
}
6 changes: 5 additions & 1 deletion TmsRunner/Options/AdapterConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down Expand Up @@ -73,7 +76,8 @@ public Config ToInternalConfig()
TmsConfigFile = TmsConfigFile,
TmsRunSettings = TmsRunSettings,
TmsAutomaticCreationTestCases = TmsAutomaticCreationTestCases,
TmsCertValidation = TmsCertValidation
TmsCertValidation = TmsCertValidation,
TmsLabelsOfTestsToRun = TmsLabelsOfTestsToRun
};
}
}
12 changes: 10 additions & 2 deletions TmsRunner/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,25 @@ public static async Task<int> 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;
}
}
Expand Down Expand Up @@ -112,9 +118,11 @@ private static AdapterConfig GetAdapterConfiguration(IEnumerable<string> args)
TmsTestRunId = ac.TmsTestRunId,
TmsTestRunName = ac.TmsTestRunName,
TmsAdapterMode = ac.TmsAdapterMode,
TmsConfigFile = ac.TmsConfigFile
TmsConfigFile = ac.TmsConfigFile,
TmsLabelsOfTestsToRun = ac.TmsLabelsOfTestsToRun
};
});

return config;
}
}
36 changes: 35 additions & 1 deletion TmsRunner/Services/FilterService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Tms.Adapter.Attributes;
using Tms.Adapter.Utils;
using TmsRunner.Extensions;
using TmsRunner.Options;

namespace TmsRunner.Services;

Expand Down Expand Up @@ -69,4 +70,37 @@ public List<TestCase> FilterTestCases(

return testCasesToRun;
}
}

public List<TestCase> FilterTestCasesByLabels(
AdapterConfig config,
IEnumerable<TestCase> testCases)
{
var labelsToRun = config.TmsLabelsOfTestsToRun.Split(',').Select(x => x.Trim()).ToList();
var testCasesName = testCases.Select(t => t.FullyQualifiedName);
var testCasesToRun = new List<TestCase>();
var assembly = Assembly.LoadFrom(config.TestAssemblyPath);
var testMethods = new List<MethodInfo>(
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;
}
}
8 changes: 7 additions & 1 deletion TmsRunner/TmsRunner.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>1.0.4</Version>
<Version>1.0.5</Version>
<OutputType>Exe</OutputType>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
Expand Down Expand Up @@ -49,4 +49,10 @@
<Compile Remove="Models\StepResult.cs" />
</ItemGroup>

<ItemGroup>
<None Update="Properties\launchSettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>

0 comments on commit 7799fcb

Please sign in to comment.