Skip to content

Commit

Permalink
before system test
Browse files Browse the repository at this point in the history
  • Loading branch information
ekhatko committed Feb 19, 2014
1 parent a06989d commit 948807f
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 32 deletions.
8 changes: 8 additions & 0 deletions App.config
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,12 @@
<add key="project" value="cmd" />
<add key="testplan" value="bestplan" />
</appSettings>
<system.diagnostics>
<trace autoflush="true" indentsize="4">
<listeners>
<add name="myListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="tfs_cli.log" />
<remove name="Default" />
</listeners>
</trace>
</system.diagnostics>
</configuration>
8 changes: 4 additions & 4 deletions CLAPOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
writer.CreateOutput(); }

[Verb(Aliases = "features",
Description = "Exports TFS tests from provided project and testplan to Cucumber features. Please fill config file beforehand\nExample usage: tfs_cli get"
Description = "Exports TFS tests from provided project and testplan to Cucumber features. Please fill config file beforehand\nExample usage: tfs_cli features"
)]
static void get_features(
[DefaultValue("features"), DescriptionAttribute("Folder for features export")] string output,
[RequiredAttribute, DescriptionAttribute("Folder for features export")] [DirectoryExists] string output,
[AliasesAttribute("tp"), DefaultValue(""), DescriptionAttribute("Testplan to get tests from (overrides .config option)")] string testplan
)
{
if (testplan != "")
_conData.setTestPlan(testplan);
FeatureWriter writer = new FeatureWriter(output, _conData, new FeatureBuilder());
_conData.setTestPlan(testplan);
TestsWriter writer = new FeatureWriter(output, _conData, new FeatureBuilder());
writer.CreateOutput();
} [Verb(Aliases = "upd", Description = "Updates TFS test with provided values. Please fill config file beforehand\nExample usage: tfs_cli.exe u /rt=\"my test run\" /ts=\"testsuite\" /tn=\"test name\" /to=Passed /duration=1")] static void update_test( [AliasesAttribute("rconf"), DescriptionAttribute("Run configuration"), DefaultValue("tfs_cli")] string run_config, [AliasesAttribute("rt"), RequiredAttribute, DescriptionAttribute("Run title")] string run_title, [AliasesAttribute("rb"), DescriptionAttribute("Run build number"), DefaultValue("0")] string run_build_number, [AliasesAttribute("rc"), DescriptionAttribute("Run comment"), DefaultValue("tfs_cli run")] string run_comment, [AliasesAttribute("ra"), DescriptionAttribute("Run attachment. E.g. overall run report")] [FileExists] string run_attachment, [AliasesAttribute("ts"), DescriptionAttribute("Test suite name")] string test_suite_name, [RequiredAttribute, AliasesAttribute("tn"), DescriptionAttribute("Test name")] string test_name, [RequiredAttribute, AliasesAttribute("to"), DescriptionAttribute("Test outcome (result). Available: Aborted, Blocked, Error, Failed, Inconclusive, None, NotApplicable, NotExecuted, Passed, Paused, Timeout, Unspecified, Warning")] string test_outcome, [AliasesAttribute("tc"), DescriptionAttribute("Test comment"), DefaultValue("tfs_cli test run")] string test_comment,
[AliasesAttribute("td"), RequiredAttribute, DescriptionAttribute("Test duration (ms)")] string duration, [AliasesAttribute("tft"), DescriptionAttribute("Test failure type. Available: KnowIssue, NewIssue, None, Regression, Unknown"), DefaultValue("None")] string test_failure_type, [AliasesAttribute("tem"), DescriptionAttribute("Test error message"), DefaultValue("")] string test_error_message, [AliasesAttribute("ta"), DescriptionAttribute("Test attachment. E.g. test run report")] [FileExists] string test_attachment, [AliasesAttribute("tp"), DefaultValue(""), DescriptionAttribute("Testplan to get tests from (overrides .config option)")] string testplan ) {
Expand Down
3 changes: 2 additions & 1 deletion CredentialConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ public void Connect()
new NetworkCredential(_connData.User(), _connData.Password(), _connData.Domen()) :
null;

_tfs = new TfsTeamProjectCollection(new Uri(_connData.Url()), cred);
_tfs = new TfsTeamProjectCollection(new Uri(_connData.Url()), cred);
_tfs.Authenticate();
TfsCliHelper.Debug(string.Format("Connection: \"{0}\" \"{1}\"", _tfs.Name, _tfs.Uri));
if (!_tfs.HasAuthenticated)
{
TfsCliHelper.ExitWithError("Could not authenticate to TFS");
Expand Down
37 changes: 26 additions & 11 deletions FeatureBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ namespace tfs_cli
{
class FeatureBuilder : ITfsCliBuilder
{
private static string sep = Path.PathSeparator.ToString();
private static string sep = Path.DirectorySeparatorChar.ToString();
private static string featuresDir = "features";
private static string featureHeader = "Feature: ";
private static string scenarioHeader = "Scenario: ";
private static string fileExtension = ".feature";
private static string prefix = " ";
private static string doublePrefix = " ";
private static string lb = Environment.NewLine;

private string tmp = Path.GetTempPath()+sep+featuresDir;

Expand All @@ -27,19 +29,24 @@ public void Append(ITestSuiteBase suite)
{
try
{
if (Directory.Exists(tmp))
Directory.Delete(tmp);
Directory.CreateDirectory(tmp);
StreamWriter file = new StreamWriter(tmp + sep + suite.Title);
if (!Directory.Exists(tmp))
Directory.CreateDirectory(tmp);
TfsCliHelper.Debug(string.Format("TmpDir: \"{0}\"", tmp));
string featureName = tmp + sep + suite.Title + fileExtension;
TfsCliHelper.Debug(string.Format("NewFeature: \"{0}\"", featureName));
StreamWriter file = new StreamWriter(featureName);
file.WriteLine(featureHeader + suite.Title);
file.WriteLine(suite.Description ?? "");
foreach (ITestSuiteEntry entry in suite.TestCases)
{
TfsCliHelper.Debug(string.Format("AddSuite: \"{0}\"", suite.Title));
Append(entry.TestCase, file);
}
file.Close();
}
catch (IOException e)
{
TfsCliHelper.ExitWithError(string.Format("Could not create feature files: {0}", e.Message));
}
TfsCliHelper.ExitWithError(string.Format("Could not create feature files in TMP dir: {0}\nError: {1}", tmp, e.Message));
}
}
public void Header(string url, string project, string testplan){}

Expand All @@ -49,19 +56,27 @@ public string GetFeatureFilesPath(){

private void Append(ITestCase test, StreamWriter file)
{
TfsCliHelper.Debug(string.Format("AddTest: \"{0}\"", test.Title));
file.WriteLine(prefix + scenarioHeader + test.Title);
foreach (ITestAction action in test.Actions)
Append((ITestStep)action, file);
}

private void Append(ITestStep step, StreamWriter file)
{
string act = TfsCliHelper.fromHtml(step.Title);
string er = TfsCliHelper.fromHtml(step.Title);
string act = TfsCliHelper.toFeatureStep(TfsCliHelper.fromHtml(step.Title), doublePrefix, lb);
string er = TfsCliHelper.toFeatureStep(TfsCliHelper.fromHtml(step.ExpectedResult), doublePrefix, lb);

if (act != "")
{
TfsCliHelper.Debug(string.Format("AddAction: \"{0}\"", act));
file.WriteLine(doublePrefix + act);
if(er != "")
}
if (er != "")
{
TfsCliHelper.Debug(string.Format("AddExpectedResult: \"{0}\"", er));
file.WriteLine(doublePrefix + er);
}
}

}
Expand Down
9 changes: 6 additions & 3 deletions FeatureWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ public FeatureWriter(string output, ConnectionData con, ITfsCliBuilder builder)

protected override void WriteToOutput()
{
string from = ((FeatureBuilder)_builder).GetFeatureFilesPath();
try
{
foreach (var file in Directory.GetFiles(((FeatureBuilder)_builder).GetFeatureFilesPath()))
File.Copy(file, Path.Combine(_output, Path.GetFileName(file)));
TfsCliHelper.Debug(string.Format("CopyFeaturesTo: \"{0}\"", _output));
foreach (var file in Directory.GetFiles(from))
File.Copy(file, Path.Combine(_output, Path.GetFileName(file)), true);
//Directory.Delete(from, true);
}
catch (IOException e)
{
TfsCliHelper.ExitWithError(string.Format("Could not create feature files: {0}", e.Message));
TfsCliHelper.ExitWithError(string.Format("Could not copy feature files from {0} to {1}.\nError: {2}", from, _output, e.Message.ToString()));
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions JunitReportParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ class JunitReportParser
private Dictionary<IRunResultProvider, List<ITestResultProvider>> _runs = new Dictionary<IRunResultProvider, List<ITestResultProvider>>();
public JunitReportParser(string report)
{
TfsCliHelper.Debug(string.Format("JunitParseFile: \"{0}\"", report));
_doc = XDocument.Load(report);
XElement ts = _doc.Root.Element("testsuite");
string run_comment = String.Concat(ts.Attributes());
var testcases = _doc.Root.Element("testsuite").Elements("testcase");
foreach (var testcase in testcases)
{
TfsCliHelper.Debug(string.Format("JunitParseTest: \"{0}\"", testcase.Name));
// parsing
string suite = testcase.Attribute("classname").Value;
string test = testcase.Attribute("name").Value;
Expand Down
1 change: 1 addition & 0 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Program
{
static void Main(string[] args)
{
TfsCliHelper.Debug(string.Format("Start: \"{0}\"", DateTime.Now.ToLongDateString()));
// Init provided config
CLAPOptions.readConfig();
// Run CLAP app
Expand Down
2 changes: 1 addition & 1 deletion TestsWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void CreateOutput()

TfsApi tfsapi = new TfsApi(connector.Collection(), _conData);
foreach (var suite in tfsapi.GetSuites())
{
{
_builder.Append(suite);
}
WriteToOutput();
Expand Down
42 changes: 35 additions & 7 deletions TfsCliHelper.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;

namespace tfs_cli
{
class TfsCliHelper
{
{
private static TraceSwitch _logger = new TraceSwitch("tfs_cli", "tfs_cli log", TraceLevel.Error.ToString());
public static void ExitWithError(string message)
{
{
Trace.WriteLine(message);
Console.WriteLine(message);
Environment.Exit(1);
}
Expand All @@ -18,6 +22,30 @@ public static string fromHtml(string htmlText)
htmlText = htmlText.Replace("</P><P>", Environment.NewLine);
htmlText = System.Text.RegularExpressions.Regex.Replace(htmlText, @"(?></?\w+)(?>(?:[^>'""]+|'[^']*'|""[^""]*"")*)>", String.Empty);
return htmlText;
}
}

public static string toFeatureStep(string inp, string prefix, string lb)
{
string[] inpAry = Regex.Split(inp, lb);
if (inpAry.Length == 1)
return inp;
StringBuilder sb = new StringBuilder(inpAry[0] + lb);
for (int i = 1; i < inpAry.Length - 1; i++)
sb.Append(prefix + inpAry[i] + lb);
sb.Append(prefix + inpAry[inpAry.Length - 1]);
return sb.ToString();
}

public static void Info(string message)
{
Trace.WriteLineIf(_logger.Level == TraceLevel.Info, message);
Console.WriteLine(message);
}

public static void Debug(string message)
{
Trace.WriteLineIf(_logger.Level == TraceLevel.Verbose, message);
Console.WriteLine(message);
}
}
}
10 changes: 6 additions & 4 deletions XmlBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public XElement DocRoot()
}

public void Append(ITestSuiteBase suite)
{
{
TfsCliHelper.Debug(string.Format("AddSuite: \"{0}\"", suite.Title));
XElement xmlsuite = new XElement("testsuite");
xmlsuite.Add(
new XAttribute("id", suite.Id),
Expand All @@ -45,7 +46,8 @@ public void Header(string url, string project, string testplan) {
}

private void Append(ITestCase test, XElement root)
{
{
TfsCliHelper.Debug(string.Format("AddTest: \"{0}\"", test.Title));
XElement xmltest = new XElement("test");
xmltest.Add(
new XAttribute("id", test.Id),
Expand All @@ -61,8 +63,8 @@ private void Append(ITestCase test, XElement root)
foreach (ITestAction action in test.Actions)
{
XElement a = new XElement("action");
ITestStep step = (ITestStep)action;

ITestStep step = (ITestStep)action;
TfsCliHelper.Debug(string.Format("AddAction: \"{0}\"-\"{1}\"", TfsCliHelper.fromHtml(step.Title), TfsCliHelper.fromHtml(step.ExpectedResult)));
a.Add(
new XAttribute("id", action.Id),
new XAttribute("action", TfsCliHelper.fromHtml(step.Title)),
Expand Down
3 changes: 2 additions & 1 deletion XmlFileWriter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;using System.Collections.Generic;using System.IO;namespace tfs_cli{ class XmlFileWriter : TestsWriter { public XmlFileWriter(string output, ConnectionData con, ITfsCliBuilder builder) : base(output, con, builder){}

protected override void WriteToOutput()
{ StreamWriter output; try { output = new StreamWriter(_output); output.Write(_builder.Finalize()); output.Close(); } catch (Exception) { TfsCliHelper.ExitWithError(string.Format("Unable to write results to file: {0}", _output)); } } }}
{ StreamWriter output; try {
TfsCliHelper.Debug(string.Format("WriteXML: \"{0}\"", _output)); output = new StreamWriter(_output); output.Write(_builder.Finalize()); output.Close(); } catch (Exception) { TfsCliHelper.ExitWithError(string.Format("Unable to write results to file: {0}", _output)); } } }}
Expand Down

0 comments on commit 948807f

Please sign in to comment.