दृष्टिकोण
आप प्रतिबिंब का उपयोग अपने निजी परीक्षण तरीकों को लागू करने का सहारा सकता है: आप एक ही सार्वजनिक NUnit परीक्षा पद्धति है जिसमें सभी निजी तरीकों से अधिक लूप होगा विधानसभा टेस्ट विशेषता के साथ उन लोगों लागू। इस दृष्टिकोण के लिए बड़ा नीचे की तरफ यह है कि आप एक समय में केवल एक असफल परीक्षण विधि देख सकते हैं (लेकिन हो सकता है कि आप इसे ठीक करने के लिए पैरामीटर परीक्षणों का उपयोग करने जैसे रचनात्मक कुछ देख सकें)।
उदाहरण
Program.fsi
namespace MyNs
module Program =
val visibleMethod: int -> int
Program.fs
namespace MyNs
open NUnit.Framework
module Program =
let implMethod1 x y =
x + y
[<Test>]
let testImpleMethod1() =
Assert.AreEqual(implMethod1 1 1, 2)
let implMethod2 x y z =
x + y + z
[<Test>]
let testImpleMethod2() =
Assert.AreEqual(implMethod2 1 1 1, 3)
let implMethod3 x y z r =
x + y + z + r
[<Test>]
let testImpleMethod3() =
Assert.AreEqual(implMethod3 1 1 1 1, -1)
let implMethod4 x y z r s =
x + y + z + r + s
[<Test>]
let testImpleMethod4() =
Assert.AreEqual(implMethod4 1 1 1 1 1, 5)
let visibleMethod x =
implMethod1 x x
+ implMethod2 x x x
+ implMethod3 x x x x
TestProxy।FS (हमारे "दृष्टिकोण" के कार्यान्वयन)
module TestProxy
open NUnit.Framework
[<Test>]
let run() =
///we only want static (i.e. let bound functions of a module),
///non-public methods (exclude any public methods, including this method,
///since those will not be skipped by nunit)
let bindingFlags = System.Reflection.BindingFlags.Static ||| System.Reflection.BindingFlags.NonPublic
///returns true if the given obj is of type TestAttribute, the attribute used for marking nunit test methods
let isTestAttr (attr:obj) =
match attr with
| :? NUnit.Framework.TestAttribute -> true
| _ -> false
let assm = System.Reflection.Assembly.GetExecutingAssembly()
let tys = assm.GetTypes()
let mutable count = 0
for ty in tys do
let methods = ty.GetMethods(bindingFlags)
for mi in methods do
let attrs = mi.GetCustomAttributes(false)
if attrs |> Array.exists isTestAttr then
//using stdout w/ flush instead of printf to ensure messages printed to screen in sequence
stdout.Write(sprintf "running test `%s`..." mi.Name)
stdout.Flush()
mi.Invoke(null,null) |> ignore
stdout.WriteLine("passed")
count <- count + 1
stdout.WriteLine(sprintf "All %i tests passed." count)
उदाहरण आउटपुट (TestDriven.NET का प्रयोग करके)
सूचना हम कभी नहीं testImplMethod4 को पाने के बाद से यह testImpleMethod3 पर विफल रहता है:
running test `testImpleMethod1`...passed
running test `testImpleMethod2`...passed
running test `testImpleMethod3`...Test 'TestProxy.run' failed: System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation.
----> NUnit.Framework.AssertionException : Expected: 4
But was: -1
at System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeType typeOwner)
at System.RuntimeMethodHandle.InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeType typeOwner)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
C:\Users\Stephen\Documents\Visual Studio 2010\Projects\FsOverflow\FsOverflow\TestProxy.fs(29,0): at TestProxy.run()
--AssertionException
C:\Users\Stephen\Documents\Visual Studio 2010\Projects\FsOverflow\FsOverflow\Program.fs(25,0): at MyNs.Program.testImpleMethod3()
0 passed, 1 failed, 4 skipped (see 'Task List'), took 0.41 seconds (NUnit 2.5.10).
आप उपयोग करने की कोशिश है InternalsVisibleTo? http://devlicio.us/blogs/derik_whittaker/archive/2007/04/09/internalsvisibleto-testing-internal-methods-in-net-2-0.aspx –
मुझे लगता है कि मेरी समस्या थोड़ा अलग है। मैं पहले से ही परीक्षणों को संकलित कर सकता हूं। हालांकि, अगर मैं उन्हें मॉड्यूल से निर्यात नहीं करता हूं तो मैं उन्हें नहीं चला सकता। मुझे लगता है कि विशेषता मदद करेगी अगर मैं यह कह सकूं कि सभी आंतरिक एनयूनीट तक पहुंच योग्य हैं। लेकिन मुझे नहीं पता कि इसके साथ कहां से शुरुआत करें, और न ही - ऐसा लगता है - Google करता है। परीक्षणों को एक अलग फ़ाइल में ले जाने के बाद मैं स्पष्ट रूप से विशेषता का उपयोग कर सकता हूं, लेकिन यह एक बुरी बुराई है। मैं उन्हें निष्पादन योग्य दस्तावेज के रूप में उपयोग करता हूं, इसलिए उन्हें वास्तव में कोड के करीब होना चाहिए। उत्तर के लिए बहुत - बहुत धन्यवाद। – user1002059
यदि यह संकलित करता है, तो इसे चलाना चाहिए। जब तक आप एक ही असेंबली में वास्तविक कोड के रूप में परीक्षण नहीं करते हैं, या ऐसा कुछ नहीं। –