How to use Write method of NUnitLite.TcpWriter class

Best Nunit code snippet using NUnitLite.TcpWriter.Write

Run Nunit automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

ConfigurableTestActivity.cs

Source: ConfigurableTestActivity.cs Github

copy
1namespace MobileSDKAndroidTests
2{
3  using System;
4  using System.IO;
5  using System.Linq;
6  using System.Reflection;
7  using Android.OS;
8  using Android.Util;
9  using NUnit.Framework.Internal;
10  using NUnitLite.Runner;
11  using Xamarin.Android.NUnitLite;
12
13  public abstract class ConfigurableTestActivity : TestSuiteActivity
14  {
15    private readonly string tag = typeof(ConfigurableTestActivity).Name;
16    private DateTime testsStartTime;
17
18    protected virtual bool IsAutomated
19    {
20      get
21      {
22        return TestResultsConfig.IsAutomated;
23      }
24    }
25
26    protected TextWriter TextWriter
27    {
28      get
29      {
30        if (TestResultsConfig.IsRemote && TestResultsConfig.TestsResultsFormat.Equals("plain_text"))
31        {
32          return this.NetworkWriter;
33        }
34        else
35        {
36          return Console.Out;
37        }
38      }
39    }
40
41    protected Assembly TestsAssembly
42    {
43      get
44      {
45        return Assembly.GetExecutingAssembly();
46      }
47    }
48
49    protected override void OnCreate(Bundle bundle)
50    {
51      this.SetTextWriterForAndroidTestRunner();
52
53      this.AddTest(TestsAssembly);
54
55      base.OnCreate(bundle);
56    }
57
58    protected override void OnResume()
59    {
60      base.OnResume();
61
62      if (IsAutomated)
63      {
64        this.RunTestsAndPublish();
65      }
66    }
67
68    public void RunTestsAndPublish()
69    {
70      var testResults = this.RunTests();
71
72      this.PublishResults(testResults);
73    }
74
75    protected virtual void PublishResults(TestResult testResults)
76    {
77      TestResultsConfig.PrintConfig();
78
79      Log.Info(this.tag, "Publishing results : " + DateTime.Now +
80                    "\nTotal count : {0}, Failed : {1}",
81        testResults.AssertCount,
82        testResults.FailCount);
83
84      if (TestResultsConfig.IsRemote)
85      {
86        switch (TestResultsConfig.TestsResultsFormat)
87        {
88          case "plain_text":
89            //          We already published test results because in this case we publish each test results separately. See SetTextWriterForAndroidTestRunner() method.
90            return;
91          case "nunit2":
92            var nunit2Writer = new NUnit2XmlOutputWriter(this.testsStartTime);
93            var tcpwriter = this.NetworkWriter;
94
95            nunit2Writer.WriteResultFile(testResults, tcpwriter);
96            tcpwriter.Close();
97
98            Log.Info(this.tag, "Published tests results in nunit2 format");
99            return;
100          case "nunit3":
101            var nunit3Writer = new NUnit3XmlOutputWriter(this.testsStartTime);
102            var newtworkWriter = this.NetworkWriter;
103
104            nunit3Writer.WriteResultFile(testResults, newtworkWriter);
105            newtworkWriter.Close();
106
107            Log.Info(this.tag, "Published tests results in nunit3 format");
108            return;
109        }
110      }
111      else
112      {
113        //      If we don't need to send test results to remote server, just return.
114        return;
115      }
116    }
117
118    protected TestResult RunTests()
119    {
120      var runMethod = this.GetAndroidRunner().GetType().GetMethod("Run", BindingFlags.Public | BindingFlags.Instance);
121      this.testsStartTime = DateTime.Now;
122
123      Log.Info(this.tag, "Running tests: " + this.testsStartTime);
124      var result = runMethod.Invoke(this.GetAndroidRunner(), new object[]
125      {
126        this.CurrentTest
127      });
128
129      return (TestResult)result;
130    }
131
132    private void SetTextWriterForAndroidTestRunner()
133    {
134      var value = this.GetAndroidRunner();
135
136      var method = this.GetSetWriterMethod();
137      method.Invoke(value, new object[] { this.TextWriter });
138    }
139
140    protected TextWriter NetworkWriter
141    {
142      get
143      {
144        var message = string.Format("[{0}] Sending results to {1}:{2}", DateTime.Now, TestResultsConfig.RemoteServerIp, TestResultsConfig.RemoteServerPort);
145
146        Log.Debug(this.GetType().Name, message);
147        try
148        {
149          return new TcpTextWriter(hostName: TestResultsConfig.RemoteServerIp, port: TestResultsConfig.RemoteServerPort);
150        }
151        catch (System.Exception exception)
152        {
153          var debugMessage = string.Format("Failed to connect to {0}:{1}.\n{2}", TestResultsConfig.RemoteServerIp, TestResultsConfig.RemoteServerPort, exception);
154
155          Log.Debug(GetType().Name, debugMessage);
156          throw;
157        }
158      }
159    }
160
161    private Test CurrentTest
162    {
163      get
164      {
165        var currentTestProperty = typeof(TestSuiteActivity).GetField("current_test", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
166        return (Test)currentTestProperty.GetValue(this);
167      }
168    }
169
170    protected object GetAndroidRunner()
171    {
172      Assembly ass = Assembly.GetAssembly(typeof(TestSuiteActivity));
173      Type type = ass.GetType("Xamarin.Android.NUnitLite.AndroidRunner");
174
175      FieldInfo info = type.GetField("runner", BindingFlags.NonPublic | BindingFlags.Static);
176      dynamic value = info.GetValue(null);
177
178      return value;
179    }
180
181    private MethodInfo GetSetWriterMethod()
182    {
183      Assembly ass = Assembly.GetAssembly(typeof(TestSuiteActivity));
184      Type type = ass.GetType("Xamarin.Android.NUnitLite.AndroidRunner");
185
186      MethodInfo[] methodInfos = type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
187
188      return methodInfos.FirstOrDefault(methodInfo => methodInfo.Name.Equals("set_Writer"));
189    }
190  }
191}
Full Screen

runner.cs

Source: runner.cs Github

copy
1using System;
2using System.IO;
3using System.Linq;
4using System.Text;
5using System.Net.Sockets;
6using NUnitLite.Runner;
7using NUnit.Framework.Internal;
8
9    class TcpWriter : TextWriter
10    {
11        private string hostName;
12        private int port;
13
14        private TcpClient client;
15        private NetworkStream stream;
16        private StreamWriter writer;
17
18        public TcpWriter(string hostName, int port)
19        {
20            this.hostName = hostName;
21            this.port = port;
22            this.client = new TcpClient(hostName, port);
23            this.stream = client.GetStream();
24            this.writer = new StreamWriter(stream);
25        }
26
27        public override void Write(char value)
28        {
29            writer.Write(value);
30        }
31
32        public override void Write(string value)
33        {
34            writer.Write(value);
35        }
36
37        public override void WriteLine(string value)
38        {
39            writer.WriteLine(value);
40            writer.Flush();
41        }
42
43        public override System.Text.Encoding Encoding
44        {
45            get { return System.Text.Encoding.Default; }
46        }
47    }
48
49public class TestRunner
50{
51	public static int Main(string[] args) {
52		TextUI runner;
53
54		// First argument is the connection string
55		if (args [0].StartsWith ("tcp:")) {
56			var parts = args [0].Split (':');
57			if (parts.Length != 3)
58				throw new Exception ();
59			string host = parts [1];
60			string port = parts [2];
61			args = args.Skip (1).ToArray ();
62
63			Console.WriteLine ($"Connecting to harness at {host}:{port}.");
64			runner = new TextUI (new TcpWriter (host, Int32.Parse (port)));
65		} else {
66			runner = new TextUI ();
67		}
68		runner.Execute (args);
69            
70		return (runner.Failure ? 1 : 0);
71    }
72}
73
Full Screen

TcpWriter.cs

Source: TcpWriter.cs Github

copy
1using System.IO;
2using System.Text;
3
4namespace NUnitLite.Runner
5{
6	internal class TcpWriter : TextWriter
7	{
8		public override Encoding Encoding => Encoding.Default;
9
10		public TcpWriter(string hostName, int port)
11		{
12		}
13
14		public override void Write(char value)
15		{
16		}
17
18		public override void Write(string value)
19		{
20		}
21
22		public override void WriteLine(string value)
23		{
24		}
25	}
26}
27
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Most used method in TcpWriter

Trigger Write code on LambdaTest Cloud Grid

Execute automation tests with Write on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)