.NET's Attribute on unit testing

zhaozj2021-02-11  226

.NET's Attribute on unit testing

---------------------------- Gao Wei (net name DRCMM, W-GAO @ 263.net) compiling this article Welcome to everyone, welcome everyone Excall with me. Please keep the above statement when reprint, thank you! ----------------------------

Description: Read the "Using Unit Test Tools in .NET Environment", it is just a good article in IEEE Software Magazine. After adapting, it is adapted, and it is available for all netizens. Also transrty, attribute is generally translated into "attributes", to avoid confusion, and attribute remains uncharged.

Microsoft introduces Attribute in the .NET framework, which is a method of adding "declarative information", also called metadata. An entity that can be added attribute includes: classes, methods, property, class variables, etc. In .NET, you can add Attribute to Assembly, different types of Attribute, which describe different aspects of Assembly. Such as: Idential Attribute (name, version, etc.) used to describe the recognition feature (name, version, etc.) of the assembly, and the information class Attribute is used to provide more product and company information, declare that attribute is used to describe configuration information, strong name Attribute is used to describe AssemblyMbly Whether the signature of the public key encryption is used. The application can read this information at runtime, which controls the interaction of itself, such as serialization, security, etc. according to this information.

.NET's Attribute and Java tag interface (MARKER Interface)

Marker Interface is a common design technique in Java. The so-called tag interface is an interface that does not contain any Method or Field, which only one use is to facilitate the Java Virtual Machine (JVM) to identify if a class has a particular Attribute. Such an example: public interface serializable {} We write class If you can serve, this interface must be implemented: Public Class SerializableClass Implements Serializable As a developer, we sometimes need to control certain behaviors related to serialization. However, in Java, serializable acts as an interface representing a serialization contract, but it is not explicitly associated with these behaviors. When the program requests JVM to run a class sequence, JVM will view this class to implement the serializable interface, and it also views whether the class defines but does not directly declares, such as readResolve, ReadObject, or WriteObject, etc. related to serialization interface. method. JVM is based on naming specifications and method prototypes, to locate these methods via reflection, once they are found. However, the Serializable interface itself does not explicitly specify these methods. Because of the need to serialize the interface, it is possible to implement these methods in the simplest form, which is completely unnecessary. The method associated with it is not explicitly designated in the interface, then the prototype of these methods may be specified in his place. It can be seen that this processing mechanism of Java is relatively easy. Worse, it is impossible to identify it as an error when compiling. .NET's solution to this problem is an explicit statement ("" With a pleasure, you shout "? ^ _ ^). The simplest example, assuming that the programmer needs to serialize an object with the serialization functionality provided by the system, you can mark this class with the class level attribute that the class level, declare the serialization function provided by the system. Such an example: [serializable ()] public class myclass {} is only marked in a class-sequentialization that does not do anything. If the programmer needs to fully control the serialization process, you must implement an interface of iSerializable to specify a method for controlling the serialization process. In the following example: [Serializable ()] public class MyClass: ISerializable {public MyClass (SerializationInfo info, StreamingContext context) {// ...} public void GetObjectData (SerializationInfo info, StreamingContext context) {// ...}} running When a program requests a CLR (public language runtime) serialization, the CLR will view whether the class is marked as attribute with serializable. We can see that Java and .NET have a meaning of "heroic" in the way in handling this issue. However, .NET's application is more straightforward to Attribute, and the cost is introduced into a new language structure. Java has multiplexed an existing language structure-interface, which also achieves the same functionality as "tag interface", which has also achieved the same functionality, expressing the information that Attribute can express.

Stroustrup mentioned in the Design and Evolution of C Language, there is a tendency to reuse existing language structures in the C / C community, and reuse the tendency of existing language structures through certain design techniques, each new keyword. The introduction is quite speaking. This is actually the different design concepts and guidance philosophy of the program language. It is necessary to express the compact clean and clean thus in the program language. It requires higher design techniques, and the program language is not a plenty of puzzling makes itself self-contained. Language structures can be expressed but difficult to use, and seek an appropriate balance between bloating. Format Naming Patterns

Java usually uses naming specifications to identify a particular method. The program uses the reflection when running, and a method can be positioned by the method name. Once found, the program can call execution. For example, when the Open Source Unit Test Framework JUnit, the programmer defines a test method, the name of the method must begin with Test. The program that performs the test first verifies that this class inherits from TestCase, then uses reflection to find all methods starting with TEST. Such an example: public class myclass extends testcase {public void testsuccess () {/ * ... * /}} If you need to verify that the code is thrown out, you can use a commonly used design convention as shown below in JUnit: public class MyClass extends TestCase {public void testMyException () {try {/ * code that throws exception * / fail ( "code should have thrown MyException");} catch (MyException e) {/ * expected exception - success * /}} } This design practice is not intuitive, and in each of the test cases that appear in an abnormality, it is necessary to add such a code, which is repeated and cumbersome. This is a common situation, perhaps we can let JUnit frameworks provide direct support for this. However, relying on naming specifications to identify a way to make western situations, such as the following example: public class myclass extends testcase {public void testsuccess_expectException_myexception () {/ * ... * /}} In the above example, we are named The use of the specification is description: This is a test method, and we look forward to this method will throw an exception of MyException. It can be seen that so many additional information should make the method name to pass independently "life unbearable". The above example is a little extreme, but it does reveal the application of naming specifications, the name itself is difficult to carry too much information. In fact, JUnit does not use this method to implement the function of checking boundary conditions. There are other methods in Java (such as Javadoc's TAG) that can act as additional information, but they are not running at runtime, and they usually require pre-processing to identify and process these TAGs.

In .NET, formatting naming mode is completely unnecessary. Since the programmer can also create its own customization Attribute because in addition to the Attribute provided by the .NET framework. Customization Customization Attribute defines the same as the Attribute provided by the use and system. These attributes are not just names, they are instances of classes, and they can have additional data. Let's look at NUNIT, which is a variety of junit in the .NET platform, supports unit testing of various languages ​​on the .NET platform. NUNIT uses Attribute at the level of classes and methods, class Attribute is called TestFixTure, and the method Attribute is called Test. Such as the following example, the test executive will look for attributes for TestFixTure's methods for TestFixTure. This overall solution has an effort, no sense of break. [TestFixTure] public class myclass {[test] public void success () {/ * ... * /}} is not only this, but the scalability of this solution is more scalability, because it can have more than one attribute. And Attribute can also have additional data. For example, NUNIT has a method attribute, indicating that the method will throw an exception. This not only makes the method names may not be limited to the operating environment, but also more relevant to the content to be tested. Such an example: [TestFixTure] public class myclass {[test] [eXpectedException] public void success () {/ * would throw my exception * /}}. Attribute in NET is a runtime entity Additional declarative information is more elegant and consistent. Because the runtime entity and the supported service are interacting by declarative information, these services and attribute (ie, declarative information representation) do not have to be fixed. By using customization Attribute, .NET provides a standard mechanism to extend system built-in metadata, programmers can develop applications that can interact with CLR not support or have not yet defined. In fact, NUnit 2.0 is written using customization Attribute, which has all the flexibility we mentioned. Compared with the .NET's Attribute solution, there is a common method of attaching declarative information in Java, such as tag interface, formatting naming mode, and javadoc's TAG, which are lacking consistency, easy to errors, to today's increasingly complex applications. It is also too simple to say. The Java community has realized this problem, and JSR-175 specifies a similar feature similar to .NET existing Attribute.

转载请注明原文地址:https://www.9cbs.com/read-4368.html

New Post(0)