How to write and organize test programs using JUnit

zhaozj2021-02-17  49

JUnit Tutorial (Kent Beck, Erich Gamma) - Fanto Translation

Here is a simple tutorial to show you how to write and organize your test programs using JUnit. A simple test case. How did you write test code? The use of expressions in the debugger may be the easiest way. You can change the expression in the debugger without recompilation, you can even decide how to change after you see the object instance you see; you can also write some expressions to the standard output. The above style test has some limitations because they all need artificial judgments to analyze the results of the program operations; and some unfriendly outputs are presented to you. You can only run a debug expression at a time. If a program has too many output statements will cause you to find the results you need. JUnit Test does not require human judgment to explain, and you can run a lot of tests at a time. If you need to test some stuff, you only have to do this:

Inherited a subclass from TestCase. Rewrite the runtest () method. When you want to check a value, call the asserttrue () method, and pass it into a Boolean true value to represent the test. For example: In order to test the number of two money in the same currency unit, we contain a real value to represent the sum of these two money. As follows: public void testsimpleadd () {Money M12CHF = New Money (12, "chf"); Money M14CHF = New Money (14, "chac"); Money Expected = New Money (26, "chaf"); Money Result = M12chf.Add (M14CHF) Asserttrue (result));} If you want to write, the test you want to write is some similar, then write a template. If you want to run multiple tests, create a group. Template: What should I do when you have two or more tests that require an operation object? Testing needs to run on some of the objects already determined, these known parts are referred to as test templates. When you write a test, you usually find that the time you build a test environment (known part) is longer than you really compare the test results. To some extent, if you carefully use the constructor, you will be more easier to write the template. Anyway, many save content from shared templates. Typically, you can apply this template to some different tests. Each test case will pass a similar information or parameter to the template, then check the different results. When you write a universal template, you are what you have to do:

Generate a subclass from the TestCase. Add the instance variables you need to use to the template. Override the setup () method to instantiate the variable. Override the Teardown () method to release the permanent resources you created in the Setup () method. For example, in order to write some test cases that use 12 Swiss francs, 14 Swiss francs, $ 28 different combinations, then write a template: public class MoneyTest Extends testcase {private Money F12CHF; Private Money F14chf; private Money f28usd; protected void Setup () {F12CHF = New Money (12, "CHF"); F14CHF = New Money (14, "CHF"); F28USD = New Money (28, "USD");}} Once you have finished writing templates, then, You can write a lot of test cases. Test case When you have a template, how do you write and call a separate test case? When there is no template, the writing test case is simple - simply override the RunTest method in the anonymous subclass of TestCase. After having a template, generate the subclass of TestCase to write the code. Then, write anonymous subclasses for separate tests. However, when some tests have been written, you will notice that many of the code lines are wasted in the grammatical. Junit provides a simple way to use template write tests, as follows: 1. Provides a public void method in a class containing templates, usually: The method name begins with Test. For example: To test the sum of Moeny and Moneybag, as follows: Public void testmoneyMoneybag () {// [12 CHF] [14 CHF] [28 USD] == {[26 CHF] [28 USD]} Money Bag [] = {F26CHF, F28USD}; Moneybag Expected = New Moneybag (BAG); assertequals (expected, f12chf.add (f28usd.add (f14chf))));} Create a MoneyTest instance to run this case, as follows: New Moneytest "TestMoneyMoneybag") When this test runs, this parameter name is used to find the method you need to run. When you have multiple test cases, they can organize them into (suite) Suite. Suite (Suite) How do you can run multiple tests at a time? As long as you have two tests, you may want to run them together. You can of course only run one, but you will be tired soon. Junit offers an object, TestSuite to make it easy for you to run any more tests.

For example: only run a test case, you may perform: TestResult results = (New Moneytest ("TestMoneyMoneybag")). Run (); run two test cases, you can generate a suite first, then these two Test cases include, as follows: testsuite suite = new testsuite (); suite.addtest (New Moneytest ("TestMoneyMoneybag"); suite.addtest (New Moneytest ("TestsImpleAdd"); test result = suite.run (); You can take another way to run multiple test cases at a time, that is, let Junit extract the kit from the tool class (TestCase). You can do this by passing the class name of the TestCase to the SUITE. TestSuite Suite = New Testsuite; TestResult Result = suite.run (); Most of the manual method is that we want the suite to contain only a subset of test cases. Other situations, recommended automatic extraction test kit methods, it can avoid changing the TestSuite generation code when you have added a test case. TestSuites not only contains test cases, but it can also include any object that implements TEST interfaces. For example, you can generate a kit in your code, and I also produce one, then we can generate a kit containing the above two kits to run together. Testsuite Suite = new testsuite (); suite.addtest (kert.suite ()); suite.addtest (Erich.suite ()); test result = suite.run (); test executor) How do you run a test, And collect the resulting results? When you have a test suite, you want to run it. Juint provides a tool to define this suite to run and display the test results, you need to access your kit by Testrunner; you can use a static method Suite (), and return a Suite to complete this the work. Public static test suite () {testsuite suite = new testsuite (); suite.addtest (New Moneytest ("TestMoneyEquals")); Suite.Addtest (New MoneyTest ("TestsImpleAdd"); Return Suite;} or Extracted: Public Static Test Suite () {Return New TestSuite (MoneyTest.class);} If TestCase does not define a Suite method, the test actuator will automatically try to extract a Suite and put the method starting with Test into the kit.

JUINT provides two ways of graphics and text, start-up, respectively: java junit.awtui.teestrunner or java junit.swingui.teStrunner. The execution of graphics interface provides a window, including: an input text Box, used to type the name of the class containing the Suite method. A button startup test. A progress bar, if the test fails, he will become red from green. A list containing a failed test. When the test is not passed, Juint provides a report of a failed test at the bottom. Juint distinguishes failed and errors. Failure is expected, and uses assertion assertions to check. Errors are not expected, and the image array index is indexed. The figure below contains a failure test. When you change the code, you must restart the graphical interface window, which is cumbersome and time consuming. To avoid this, JUnit's AWT and SWING UIS can take advantage of JUnit.Runner.LoadingTestCollector, which re-reads your class every time the test is run. If you want to close this feature, remove the "Reload Classes" property option. You can find more detailed information in your help. There is a batch to launch junit. You can type Java Junit.Textui.Testrunner in the command line, follow the class name of the Suite method. This approach gives some terminal text output. Another way to start is in your TestCase class, which contains the MAIN method defined below. For example: launching MoneyTest's test executor, public static void main (string args []) {junit.textui.teestrunner.run (suite ());} You can be in the command line after you define this main method Type: Java Moneytest to run your test. Whether it is a graphic method or a text, it is confirmed whether to include junit.jar on your classpath. In this article, TestCase - Test case, Suite - suite, Testrunner - test actuator, ---- translation By

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

New Post(0)