FindDuplicateTests.ps1 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # Define the root directory containing test projects
  2. $testsDir = "./Tests"
  3. # Get all subfolders in the ./Tests directory
  4. $subfolders = Get-ChildItem -Directory $testsDir
  5. # Initialize a hashtable to track method names and their associated subfolders
  6. $methodMap = @{}
  7. # Iterate through each subfolder
  8. foreach ($subfolder in $subfolders) {
  9. $subfolderName = $subfolder.Name
  10. # Run dotnet test --list-tests to get the list of tests in the subfolder
  11. $output = dotnet test $subfolder.FullName --list-tests | Out-String
  12. # Split the output into lines and filter for lines containing a dot (indicative of test names)
  13. $testLines = $output -split "`n" | Where-Object { $_ -match "\." }
  14. # Process each test line to extract the method name
  15. foreach ($testLine in $testLines) {
  16. $trimmed = $testLine.Trim()
  17. $parts = $trimmed -split "\."
  18. $lastPart = $parts[-1]
  19. # Handle parameterized tests by extracting the method name before any parentheses
  20. if ($lastPart -match "\(") {
  21. $methodName = $lastPart.Substring(0, $lastPart.IndexOf("("))
  22. } else {
  23. $methodName = $lastPart
  24. }
  25. # Update the hashtable with the method name and subfolder
  26. if ($methodMap.ContainsKey($methodName)) {
  27. # Add the subfolder only if it’s not already listed for this method name
  28. if (-not ($methodMap[$methodName] -contains $subfolderName)) {
  29. $methodMap[$methodName] += $subfolderName
  30. }
  31. } else {
  32. $methodMap[$methodName] = @($subfolderName)
  33. }
  34. }
  35. }
  36. # Identify and display duplicated test method names
  37. foreach ($entry in $methodMap.GetEnumerator()) {
  38. if ($entry.Value.Count -gt 1) {
  39. Write-Output "Duplicated test: $($entry.Key) in folders: $($entry.Value -join ', ')"
  40. }
  41. }