Why use JUnit for testing?

后端 未结 11 1450
忘掉有多难
忘掉有多难 2020-12-12 09:59

Maybe my question is a newbie one, but I can not really understand the circumstances under which I would use junit?

Whether I write simple applications or larger one

相关标签:
11条回答
  • 2020-12-12 10:12

    The main advantage of JUnit is that it is automated rather than you manually having to check with your print outs. Each test you write stays with your system. This means that if you make a change that has an unexpected side effect your test will catch it and fail rather than you having to remember to manually test everything after each change.

    0 讨论(0)
  • 2020-12-12 10:13

    JUnit is a unit testing framework for the Java Programming Language. It is important in the test driven development, and is one of a family of unit testing frameworks collectively known as xUnit.

    JUnit promotes the idea of "first testing then coding", which emphasis on setting up the test data for a piece of code which can be tested first and then can be implemented . This approach is like "test a little, code a little, test a little, code a little..." which increases programmer productivity and stability of program code that reduces programmer stress and the time spent on debugging.

    Features JUnit is an open source framework which is used for writing & running tests.

    Provides Annotation to identify the test methods.

    Provides Assertions for testing expected results.

    Provides Test runners for running tests.

    JUnit tests allow you to write code faster which increasing quality

    JUnit is elegantly simple. It is less complex & takes less time.

    JUnit tests can be run automatically and they check their own results and provide immediate feedback. There's no need to manually comb through a report of test results.

    JUnit tests can be organized into test suites containing test cases and even other test suites.

    Junit shows test progress in a bar that is green if test is going fine and it turns red when a test fails.

    0 讨论(0)
  • 2020-12-12 10:17

    When you test using something like System.out, you're only testing a small subset of possible use-cases. This is not very thorough when you're dealing with systems that could accept a near infinite amount of different inputs.

    Unit tests are designed to allow you to quickly run tests on your application using a very large and diverse set of different data inputs. Additionally, the best unit tests also account for boundary cases, such as the data inputs that lie right on the edge of what is considered valid.

    For a human being to test all of these different inputs could take weeks whereas it could take minutes for a machine.

    Think of it like this: You're also not "testing" something that will be static. Your application is most likely going through constant changes. Therefore, these unit tests are designed to run at different points in the compile or deployment cycle. Perhaps the biggest advantage is this:

    If you break something in your code, you'll know about it right now, not after you deployed, not when a QA tester catches a bug, not when your clients have cancelled. You'll also have a better chance of fixing the glitch immediately, since it's clear that the thing that broke the part of the code in question most likely happened since your last compile. Thus, the amount of investigative work required to fix the problem is greatly reduced.

    0 讨论(0)
  • 2020-12-12 10:21

    I added some other System.out can NOT do:

    • Make each test cases independent (It's important)

      JUnit can do it: each time new test case instance will be created and @Before is called.

    • Separate testing code from source

      JUnit can do it.

    • Integration with CI

      JUnit can do it with Ant and Maven.

    • Arrange and combine test cases easily

      JUnit can do @Ignore and test suite.

    • Easy to check result

      JUnit offers many Assert methods (assertEquals, assertSame...)

    • Mock and stub make you focus on the test module.

      JUnit can do: Using mock and stub make you setup correct fixture, and focus on the test module logic.

    0 讨论(0)
  • 2020-12-12 10:22

    JUNIT : OBSERVE AND ADJUST

    Here is my perspective of JUNIT.

    JUNIT can be used to,
    1)Observe a system behaviour when a new unit is added in that system.
    2)Make adjustment in the system to welcome the "new" unit in the system.
    What? Exactly.

    Real life eg.

    When your relative visits your college hostel room,
    1) You will pretend to be more responsible.
    2) You will keep all things where they should be, like shoes in shoe rack not on chair, clothes in cupboard not on chair.
    3) You will get rid of all the contraband.
    4) you will start cleanUp in every device you posses.

    In programming terms

    System: Your code
    UNIT: new functionality.
    As JUNIT framework is used for JAVA language so JUNIT = JAVA UNIT (May be).

    Suppose a you already have a well bulletproof code, but a new requirement came and you have to add the new requirement in your code. This new requirement may break your code for some input(testcase).

    Easy way to adapt this change is using unit testing (JUNIT).
    For that you should write multiple testcases for your code when you are building your codebase. And whenever a new requirement comes you just run all the test cases to see if any test case fails. If No then you are a BadA** artist and you are ready to deploy the new code.
    If any of the testcases fail then you change your code and again run testcases until you get the green status.

    0 讨论(0)
  • 2020-12-12 10:25

    You cannot write any test case without using testing framework or else you will have to write your testing framewok to give justice to your test cases. Here are some info about JUnit Framework apart from that you can use TestNG framework .

    What is Junit?

    Junit is widely used testing framework along with Java Programming Language. You can use this automation framework for both unit testing and UI testing.It helps us define the flow of execution of our code with different Annotations. Junit is built on idea of "first testing and then coding" which helps us to increase productivity of test cases and stability of the code.

    Important Features of Junit Testing -

    1. It is open source testing framework allowing users to write and run test cases effectively.
    2. Provides various types of annotations to identify test methods.
    3. Provides different Types of Assertions to verify the results of test case execution.
    4. It also gives test runners for running tests effectively.
    5. It is very simple and hence saves time.
    6. It provides ways to organize your test cases in form of test suits.
    7. It gives test case results in simple and elegant way.
    8. You can integrate jUnit with Eclipse, Android Studio, Maven & Ant, Gradle and Jenkins
    0 讨论(0)
提交回复
热议问题