@@ -4,64 +4,252 @@ NUnit test runners for Xamarin and mobile devices
44
55## How to Use ##
66
7- The NuGet packages are nearly ready and we will likely create project templates, but until that is done,
8- you will need to build from source. For this, you will need a Xamarin trial or subscription.
7+ ** Project templates will be coming soon** , in the meantime...
8+
9+ In your solution;
10+
11+ 1 . Add a new project to your solution
12+ - ` Blank App (Android) ` for Android,
13+ - ` Blank App (iOS) ` for iOS,
14+ - ` Blank App (Windows Phone) ` for Windows Phone 8.1
15+ - ` Blank App (Universal Windows) ` for Windows 10 Universal
16+ 2 . Add the ` nunit.xamarin ` NuGet package to your projects
17+ 3 . Text files will be added to your project, open them and copy over the coresponding file in each project as appropriate.
18+ - ` MainActivity.cs.txt ` for Android,
19+ - ` AppDelegate.cs.txt ` for iOS, or
20+ - ` MainPage.xaml.txt ` and ` MainPage.xaml.cs.txt ` for WinPhone.
21+ - Windows 10 Universal doesn't currently add files, see below for what to change.
22+ 4 . Once you are done with them, you can delete the text files that were added to your project.
23+ 5 . On Windows Phone and Windows Universal, you will also need to add ` Xamarin.Forms.Forms.Init(e); ` to ` App.OnLaunched() ` .
24+ 6 . Write your unit tests in this project, or in a shared project
25+ 7 . Build and run the tests on your device or emulator
926
10- 1 . Clone this repository
11- 2 . Open ` nunit.runner.sln ` in Visual Studio with Xamarin installed, or in Xamarin Studio.
12- 3 . Create a release build of the solution.
27+ ### Android ###
1328
14- Then in your solution;
29+ ** MainActivity.cs **
1530
16- 1 . Add a new ` Blank App (Android) ` or ` Blank App (iOS) ` to your solution
17- 2 . Add NuGet packages to your project for ` NUnit 3.0.0-beta-4 ` and ` Xamarin.Forms 1.4.4.6392 `
18- 3 . Browse and add a reference to the ` nunit.runner.droid.dll ` or ` nunit.runner.ios.dll ` that you built
19- 4 . Write your unit tests in this project, or in a shared project
20- 5 . Change the base class of ` MainActivity ` on Android to ` global::Xamarin.Forms.Platform.Android.FormsApplicationActivity `
21- 6 . Change the base class of ` AppDelegate ` on iOS to ` global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate `
22- 7 . Change MainActivity.OnCreate() on Android or AppDelegate.FinishedLaunching() on iOS
23- 8 . Build and run the tests on your device or emulator
31+ ``` C#
32+ [Activity (Label = " NUnit 3.0" , MainLauncher = true , Theme = " @android:style/Theme.Holo.Light" , ConfigurationChanges = ConfigChanges .ScreenSize | ConfigChanges .Orientation )]
33+ public class MainActivity : global ::Xamarin .Forms .Platform .Android .FormsApplicationActivity
34+ {
35+ protected override void OnCreate (Bundle savedInstanceState )
36+ {
37+ base .OnCreate (savedInstanceState );
2438
25- ### Android ###
39+ global :: Xamarin .Forms .Forms .Init (this , savedInstanceState );
40+
41+ // This will load all tests within the current project
42+ var nunit = new NUnit .Runner .App ();
43+
44+ // If you want to add tests in another assembly
45+ // nunit.AddTestAssembly(typeof(MyTests).Assembly);
46+
47+ // Do you want to automatically run tests when the app starts?
48+ nunit .AutoRun = true ;
49+
50+ LoadApplication (nunit );
51+ }
52+ }
53+ ```
54+ ### iOS ###
55+
56+ ** AppDelegate.cs**
2657
2758``` C#
28- protected override void OnCreate (Bundle savedInstanceState )
59+ [Register (" AppDelegate" )]
60+ public partial class AppDelegate : global ::Xamarin .Forms .Platform .iOS .FormsApplicationDelegate
2961{
30- base .OnCreate (savedInstanceState );
62+ //
63+ // This method is invoked when the application has loaded and is ready to run. In this
64+ // method you should instantiate the window, load the UI into it and then make the window
65+ // visible.
66+ //
67+ // You have 17 seconds to return from this method, or iOS will terminate your application.
68+ //
69+ public override bool FinishedLaunching (UIApplication app , NSDictionary options )
70+ {
71+ global :: Xamarin .Forms .Forms .Init ();
72+
73+ // This will load all tests within the current project
74+ var nunit = new NUnit .Runner .App ();
75+
76+ // If you want to add tests in another assembly
77+ // nunit.AddTestAssembly(typeof(MyTests).Assembly);
78+
79+ // Do you want to automatically run tests when the app starts?
80+ nunit .AutoRun = true ;
81+
82+ LoadApplication (nunit );
83+
84+ return base .FinishedLaunching (app , options );
85+ }
86+ }
87+ ```
3188
32- global :: Xamarin . Forms . Forms . Init ( this , savedInstanceState );
89+ ### Windows Phone 8.1 ###
3390
34- // This will load all tests within the current project
35- var nunit = new NUnit .Runner .App ();
91+ ** MainPage.xaml**
3692
37- // If you want to add tests in another assembly
38- // nunit.AddTestAssembly(typof(MyTests).Assembly);
93+ ``` XML
94+ <forms : WindowsPhonePage
95+ x : Class =" Xamarin.Test.wp81.MainPage"
96+ xmlns =" http://schemas.microsoft.com/winfx/2006/xaml/presentation"
97+ xmlns : x =" http://schemas.microsoft.com/winfx/2006/xaml"
98+ xmlns : d =" http://schemas.microsoft.com/expression/blend/2008"
99+ xmlns : mc =" http://schemas.openxmlformats.org/markup-compatibility/2006"
100+ xmlns : forms =" using:Xamarin.Forms.Platform.WinRT"
101+ mc : Ignorable =" d"
102+ Background =" {ThemeResource ApplicationPageBackgroundThemeBrush}" >
39103
40- // Do you want to automatically run tests when the app starts?
41- nunit .AutoRun = true ;
104+ <Grid >
42105
43- LoadApplication (nunit );
106+ </Grid >
107+ </forms : WindowsPhonePage >
108+ ```
109+
110+ ** MainPage.xaml.cs**
111+
112+ ``` C#
113+ public sealed partial class MainPage : WindowsPhonePage
114+ {
115+ public MainPage ()
116+ {
117+ InitializeComponent ();
118+
119+ // Windows Phone will not load all tests within the current project,
120+ // you must do it explicitly below
121+ var nunit = new NUnit .Runner .App ();
122+
123+ // If you want to add tests in another assembly, add a reference and
124+ // duplicate the following line with a type from the referenced assembly
125+ nunit .AddTestAssembly (typeof (MainPage ).GetTypeInfo ().Assembly );
126+
127+ // Do you want to automatically run tests when the app starts?
128+ nunit .AutoRun = true ;
129+
130+ LoadApplication (nunit );
131+
132+ this .NavigationCacheMode = NavigationCacheMode .Required ;
133+ }
44134}
45135```
46- ### iOS ###
136+
137+ ** App.xaml.cs**
47138
48139``` C#
49- public override bool FinishedLaunching ( UIApplication app , NSDictionary options )
140+ protected override void OnLaunched ( LaunchActivatedEventArgs e )
50141{
51- global :: Xamarin .Forms .Forms .Init ();
142+ // <SNIP>
143+
144+ Frame rootFrame = Window .Current .Content as Frame ;
145+
146+ // Do not repeat app initialization when the Window already has content,
147+ // just ensure that the window is active
148+ if (rootFrame == null )
149+ {
150+ // Create a Frame to act as the navigation context and navigate to the first page
151+ rootFrame = new Frame ();
152+
153+ // TODO: change this value to a cache size that is appropriate for your application
154+ rootFrame .CacheSize = 1 ;
52155
53- // This will load all tests within the current project
54- var nunit = new NUnit . Runner . App () ;
156+ // Set the default language
157+ rootFrame . Language = Windows . Globalization . ApplicationLanguages . Languages [ 0 ] ;
55158
56- // If you want to add tests in another assembly
57- // nunit.AddTestAssembly(typof(MyTests).Assembly );
159+ // ==> ADD THIS LINE <==
160+ Xamarin . Forms . Forms . Init ( e );
58161
59- // Do you want to automatically run tests when the app starts?
60- nunit .AutoRun = true ;
162+ if (e .PreviousExecutionState == ApplicationExecutionState .Terminated )
163+ {
164+ // TODO: Load state from previously suspended application
165+ }
61166
62- LoadApplication (nunit );
167+ // Place the frame in the current Window
168+ Window .Current .Content = rootFrame ;
169+ }
63170
64- return base . FinishedLaunching ( app , options );
171+ // <SNIP>
65172 }
66173```
67174
175+
176+
177+ ### Windows 10 Universal ###
178+
179+ ** MainPage.xaml**
180+
181+ ``` XML
182+ <forms : WindowsPage
183+ x : Class =" NUnit.Runner.Tests.MainPage"
184+ xmlns =" http://schemas.microsoft.com/winfx/2006/xaml/presentation"
185+ xmlns : x =" http://schemas.microsoft.com/winfx/2006/xaml"
186+ xmlns : local =" using:NUnit.Runner.Tests"
187+ xmlns : d =" http://schemas.microsoft.com/expression/blend/2008"
188+ xmlns : mc =" http://schemas.openxmlformats.org/markup-compatibility/2006"
189+ xmlns : forms =" using:Xamarin.Forms.Platform.WinRT"
190+ mc : Ignorable =" d" >
191+
192+ <Grid Background =" {ThemeResource ApplicationPageBackgroundThemeBrush}" >
193+
194+ </Grid >
195+ </forms : WindowsPage >
196+ ```
197+
198+ ** MainPage.xaml.cs**
199+
200+ ``` C#
201+ public sealed partial class MainPage : WindowsPage
202+ {
203+ public MainPage ()
204+ {
205+ InitializeComponent ();
206+
207+ // Windows Universal will not load all tests within the current project,
208+ // you must do it explicitly below
209+ var nunit = new NUnit .Runner .App ();
210+
211+ // If you want to add tests in another assembly, add a reference and
212+ // duplicate the following line with a type from the referenced assembly
213+ nunit .AddTestAssembly (typeof (MainPage ).GetTypeInfo ().Assembly );
214+
215+ // Do you want to automatically run tests when the app starts?
216+ nunit .AutoRun = true ;
217+
218+ LoadApplication (nunit );
219+ }
220+ }
221+ ```
222+
223+ ** App.xaml.cs**
224+
225+ ``` C#
226+ protected override void OnLaunched (LaunchActivatedEventArgs e )
227+ {
228+ // <SNIP>
229+
230+ Frame rootFrame = Window .Current .Content as Frame ;
231+
232+ // Do not repeat app initialization when the Window already has content,
233+ // just ensure that the window is active
234+ if (rootFrame == null )
235+ {
236+ // Create a Frame to act as the navigation context and navigate to the first page
237+ rootFrame = new Frame ();
238+
239+ rootFrame .NavigationFailed += OnNavigationFailed ;
240+
241+ // ==> ADD THIS LINE <==
242+ Xamarin .Forms .Forms .Init (e );
243+
244+ if (e .PreviousExecutionState == ApplicationExecutionState .Terminated )
245+ {
246+ // TODO: Load state from previously suspended application
247+ }
248+
249+ // Place the frame in the current Window
250+ Window .Current .Content = rootFrame ;
251+ }
252+
253+ // <SNIP>
254+ }
255+ ```
0 commit comments