One thing that I would like to enforce in my unit tests are constraints. Examples of constraints are things like read-only properties and sealed classes. If a developer comes along and removes one of those constraints by making the property read-write, or by unsealing the class, those changes are not detectable by simply recompiling the code.
One way to enforce constraints is to write unit tests that assert their existence. To help folks out, I’ve written the beginnings of a utility class that can be used with NUnit tests. Here’s the source code for that class for folks who are interested. Feedback about these ideas would be greatly appreciated.
Public Class AssertEx : Inherits Assertion
Public Shared Sub AssertReadOnlyProperty(ByVal t As Type, ByVal propertyName As String)
Dim pi As PropertyInfo = t.GetProperty(propertyName)
Assert(String.Format("Read-only property {0} in class {1} cannot be read from", propertyName, t.FullName), pi.CanRead)
Assert(String.Format("Read-only property {0} in class {1} can be written to", propertyName, t.FullName), Not pi.CanWrite)
End Sub
Public Shared Sub AssertWriteOnlyProperty(ByVal t As Type, ByVal propertyName As String)
Dim pi As PropertyInfo = t.GetProperty(propertyName)
Assert(String.Format("Write-only property {0} in class {1} can be read from", propertyName, t.FullName), Not pi.CanRead)
Assert(String.Format("Write-only property {0} in class {1} cannot be written to", propertyName, t.FullName), pi.CanWrite)
End Sub
' TODO: Implement a visibility property assert - must be internal for example. need to use GetAccessors()
' and assert visibility based on property get/set method visibility
Public Shared Sub AssertNotInheritableClass(ByVal t As Type)
Assert(String.Format("Class {0} cannot be derived from", t.FullName), t.IsSealed)
End Sub
Public Shared Sub AssertNonSerializable(ByVal t As Type)
Assert(String.Format("Class {0} cannot be serializable", t.FullName), Not t.IsSerializable)
End Sub
' TODO: Assert that a class must be abstract
Public Shared Sub AssertNotCreatable(ByVal t As Type)
Dim cis() As ConstructorInfo = t.GetConstructors()
Dim ci As ConstructorInfo
For Each ci In cis
Assert(String.Format("Non-private constructor found in a class {0} that must not be creatable", t.FullName), ci.IsPrivate)
Next
End Sub
End Class
6 archived reader comments
Jonathan de Halleux (2004-05-06)
Cool idea,
I will integrate that into MbUnit
Anson Tsao (2004-05-06)
John,
You are scaring me, more VB code? :)
Anson
Pierre Arnaud (2004-05-07)
Good idea, indeed. But you should rather inherit from Assert rather than from Assertion which has been deprecated in NUnit 2.1, from what I understand.
Pierre
RichB (2004-05-07)
How about an assertion to make sure a method contains an assertion?
John Lam (2004-05-07)
Anson: Come on over to the dark side, it’s really quite nice over here :)
Pierre: Thanks for the tip. I’ll modify the code.
RichB: That’s actually a really interesting idea. It’s a lot more work, however, since it involves parsing IL. That said, it might be an interesting side project. Thanks for the idea!
Barry Gervin (2004-05-16)
Thoughts on adding…? AssertIsAssignableFrom(Type type, Type ancestor) AssertDoesImplement(Type class, Type interface) AssertIsSingleton(Type singleton) …maybe AssertDoesNotHavePublicConstructor
Seems like most of these things are static code analysis things....traditionally more of the domain of fxCop rather than NUnit. Of course fxCop is an across the board testing of best practices and not one off’s here and there like NUnit.