1+ // Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt
2+
3+ #if NETCOREAPP
4+ using System ;
5+ using System . Collections . Generic ;
6+ using System . Reflection ;
7+ using System . Xml ;
8+ using NUnit . Tests . Assemblies ;
9+ using NUnit . Framework ;
10+ using NUnit . Framework . Internal ;
11+ using System . Runtime . Loader ;
12+
13+ namespace NUnit . Engine . Drivers . Tests
14+ {
15+ // Functional tests of the NUnitFrameworkDriver calling into the framework.
16+ public class NUnitNetCore31DriverTests
17+ {
18+ private const string MOCK_ASSEMBLY = "mock-assembly.dll" ;
19+ private const string LOAD_MESSAGE = "Method called without calling Load first" ;
20+
21+ private IDictionary < string , object > _settings = new Dictionary < string , object > ( ) ;
22+
23+ private NUnitNetCore31Driver _driver ;
24+ private string _mockAssemblyPath ;
25+
26+ [ SetUp ]
27+ public void CreateDriver ( )
28+ {
29+ var assemblyName = typeof ( NUnit . Framework . TestAttribute ) . Assembly . GetName ( ) ;
30+ _mockAssemblyPath = System . IO . Path . Combine ( TestContext . CurrentContext . TestDirectory , MOCK_ASSEMBLY ) ;
31+ _driver = new NUnitNetCore31Driver ( ) ;
32+ }
33+
34+ [ Test ]
35+ public void Load_GoodFile_ReturnsRunnableSuite ( )
36+ {
37+ var result = XmlHelper . CreateXmlNode ( _driver . Load ( _mockAssemblyPath , _settings ) ) ;
38+
39+ Assert . That ( result . Name , Is . EqualTo ( "test-suite" ) ) ;
40+ Assert . That ( result . GetAttribute ( "type" ) , Is . EqualTo ( "Assembly" ) ) ;
41+ Assert . That ( result . GetAttribute ( "runstate" ) , Is . EqualTo ( "Runnable" ) ) ;
42+ Assert . That ( result . GetAttribute ( "testcasecount" ) , Is . EqualTo ( MockAssembly . Tests . ToString ( ) ) ) ;
43+ Assert . That ( result . SelectNodes ( "test-suite" ) . Count , Is . EqualTo ( 0 ) , "Load result should not have child tests" ) ;
44+ }
45+
46+ [ Test ]
47+ public void Explore_AfterLoad_ReturnsRunnableSuite ( )
48+ {
49+ _driver . Load ( _mockAssemblyPath , _settings ) ;
50+ var result = XmlHelper . CreateXmlNode ( _driver . Explore ( TestFilter . Empty . Text ) ) ;
51+
52+ Assert . That ( result . Name , Is . EqualTo ( "test-suite" ) ) ;
53+ Assert . That ( result . GetAttribute ( "type" ) , Is . EqualTo ( "Assembly" ) ) ;
54+ Assert . That ( result . GetAttribute ( "runstate" ) , Is . EqualTo ( "Runnable" ) ) ;
55+ Assert . That ( result . GetAttribute ( "testcasecount" ) , Is . EqualTo ( MockAssembly . Tests . ToString ( ) ) ) ;
56+ Assert . That ( result . SelectNodes ( "test-suite" ) . Count , Is . GreaterThan ( 0 ) , "Explore result should have child tests" ) ;
57+ }
58+
59+ [ Test ]
60+ public void ExploreTestsAction_WithoutLoad_ThrowsInvalidOperationException ( )
61+ {
62+ var ex = Assert . Catch ( ( ) => _driver . Explore ( TestFilter . Empty . Text ) ) ;
63+ if ( ex is System . Reflection . TargetInvocationException )
64+ ex = ex . InnerException ;
65+ Assert . That ( ex , Is . TypeOf < InvalidOperationException > ( ) ) ;
66+ Assert . That ( ex . Message , Is . EqualTo ( LOAD_MESSAGE ) ) ;
67+ }
68+
69+ [ Test ]
70+ public void CountTestsAction_AfterLoad_ReturnsCorrectCount ( )
71+ {
72+ _driver . Load ( _mockAssemblyPath , _settings ) ;
73+ Assert . That ( _driver . CountTestCases ( TestFilter . Empty . Text ) , Is . EqualTo ( MockAssembly . Tests ) ) ;
74+ }
75+
76+ [ Test ]
77+ public void CountTestsAction_WithoutLoad_ThrowsInvalidOperationException ( )
78+ {
79+ var ex = Assert . Catch ( ( ) => _driver . CountTestCases ( TestFilter . Empty . Text ) ) ;
80+ if ( ex is System . Reflection . TargetInvocationException )
81+ ex = ex . InnerException ;
82+ Assert . That ( ex , Is . TypeOf < InvalidOperationException > ( ) ) ;
83+ Assert . That ( ex . Message , Is . EqualTo ( LOAD_MESSAGE ) ) ;
84+ }
85+
86+ [ Test ]
87+ public void RunTestsAction_AfterLoad_ReturnsRunnableSuite ( )
88+ {
89+ _driver . Load ( _mockAssemblyPath , _settings ) ;
90+ var result = XmlHelper . CreateXmlNode ( _driver . Run ( new NullListener ( ) , TestFilter . Empty . Text ) ) ;
91+
92+ Assert . That ( result . Name , Is . EqualTo ( "test-suite" ) ) ;
93+ Assert . That ( result . GetAttribute ( "type" ) , Is . EqualTo ( "Assembly" ) ) ;
94+ Assert . That ( result . GetAttribute ( "runstate" ) , Is . EqualTo ( "Runnable" ) ) ;
95+ Assert . That ( result . GetAttribute ( "testcasecount" ) , Is . EqualTo ( MockAssembly . Tests . ToString ( ) ) ) ;
96+ Assert . That ( result . GetAttribute ( "result" ) , Is . EqualTo ( "Failed" ) ) ;
97+ Assert . That ( result . GetAttribute ( "passed" ) , Is . EqualTo ( MockAssembly . PassedInAttribute . ToString ( ) ) ) ;
98+ Assert . That ( result . GetAttribute ( "failed" ) , Is . EqualTo ( MockAssembly . Failed . ToString ( ) ) ) ;
99+ Assert . That ( result . GetAttribute ( "skipped" ) , Is . EqualTo ( MockAssembly . Skipped . ToString ( ) ) ) ;
100+ Assert . That ( result . GetAttribute ( "inconclusive" ) , Is . EqualTo ( MockAssembly . Inconclusive . ToString ( ) ) ) ;
101+ Assert . That ( result . SelectNodes ( "test-suite" ) . Count , Is . GreaterThan ( 0 ) , "Explore result should have child tests" ) ;
102+ }
103+
104+ [ Test ]
105+ public void RunTestsAction_WithoutLoad_ThrowsInvalidOperationException ( )
106+ {
107+ var ex = Assert . Catch ( ( ) => _driver . Run ( new NullListener ( ) , TestFilter . Empty . Text ) ) ;
108+ if ( ex is TargetInvocationException )
109+ ex = ex . InnerException ;
110+ Assert . That ( ex , Is . TypeOf < InvalidOperationException > ( ) ) ;
111+ Assert . That ( ex . Message , Is . EqualTo ( LOAD_MESSAGE ) ) ;
112+ }
113+
114+ [ Test ]
115+ public void RunTestsAction_WithInvalidFilterElement_ThrowsNUnitEngineException ( )
116+ {
117+ _driver . Load ( _mockAssemblyPath , _settings ) ;
118+
119+ var invalidFilter = "<filter><invalidElement>foo</invalidElement></filter>" ;
120+ var ex = Assert . Catch ( ( ) => _driver . Run ( new NullListener ( ) , invalidFilter ) ) ;
121+ Assert . That ( ex , Is . TypeOf < TargetInvocationException > ( ) ) ;
122+ Assert . That ( ex . InnerException , Is . TypeOf < ArgumentException > ( ) ) ;
123+ }
124+
125+ private class CallbackEventHandler : System . Web . UI . ICallbackEventHandler
126+ {
127+ private string _result ;
128+
129+ public string GetCallbackResult ( )
130+ {
131+ return _result ;
132+ }
133+
134+ public void RaiseCallbackEvent ( string eventArgument )
135+ {
136+ _result = eventArgument ;
137+ }
138+ }
139+
140+ public class NullListener : ITestEventListener
141+ {
142+ public void OnTestEvent ( string testEvent )
143+ {
144+ // No action
145+ }
146+ }
147+ }
148+ }
149+ #endif
0 commit comments