23
Comments
  • 1
    Still don't know what a unit test is...
  • 0
    @ThatDude when did u become girl..
    For a moment I thought that there is a bug in displaying Avatar.
  • 0
    @coolq
    Ha ha. Learn it any cost. So that u won't regret later
  • 1
    @sbin
    This is when they became a girl:
    https://devrant.io/rants/1006760/...

    Alright, I'll try! Can you give me a simple rundown of what a unit test is?
  • 1
    @coolq
    Unit tests are written to test a single unit (function) of a program.
    Suppose u have a function to send mail..
    function String mail(email,content){
    return "Success or failure";
    }

    In order to test this u will write a test method..
    function testMail(){
    assertEquals(obj.mail(validemail,validContent),"Expected Result);
    assertEquals (mail(invalidMail,ValidResult),"Expected Result");
    ......
    ......
    }
    Now if you run these tests and every test pass, u r good to go. Else u should check mail logic. This comes handy if u decide to update logic or API of mail in future, u just can run previous tests and check it without worries.

    PS: written using my mobile, may contain multiple typos
  • 0
  • 1
    @sbin @nahson
    Thank you both for explaining!

    I need to get writing these ASAP.
    It sounds quite clever actually!
  • 1
    @sbin I'd argue that that's closer to an integration test than a unit test. As unit test says, it's a single unit of code being tested. Usually for logic in a method. Every side effect in your method are usually mocked, so you only have the logic left (the actual thing to test) The moment you have a call to an API that you don't mock, if it's checking if a mail got correctly send or something got persisted in a DB, I'd argue that it's an integration test :)
    I'm not meaning to be a prick here, I just think there's a fine line between the two that I just had to explain :)
  • 1
    @coolq As a continuation of my previous comment. Say you have implemented something that changes the format of a given date formatted as a string, and you know it should return a dateobject or another string that looks different. That would be a good method for a unit test, since you're testing the logic of that method. It could also be a calculation function that you wrote :) I hope you see the difference between integration and unit tests (if you've read my previous comment that is)
  • 0
    @aaxa
    Thanks dude.. just started using Unit tests
    I think I should relearn concept of unit test and integration test from scratch :)
  • 0
    @aaxa
    I think I understand.

    So a unit test is mocking up how a method should be used, and testing it like so. Like if I had a function that adds an 'A' to the end of the inputted string, I could call the function and test the output? Is that a unit test?

    And is an integration test where to check to see if your method works with external APIs and services? Or even just other methods?

    Thanks for your help btw 😄
  • 0
    @coolq Pretty much yeah :) (and I'm happy to help) A note on mocking and unit tests: Mocking is something you do to remove dependencies of a method. So, if you have a method that performs some logic on some data it receives from a REST API, you mock the REST call, and provide some dummy data for what it needs. Then, when you're testing, you're only testing the logic the method performs, and not wether or not it receives valid data from the REST API :)
    The test where you don't mock the REST API, are the integration test. Here you will be dependent on the data and that the REST API is actually available :)
    Does it make sense?
  • 1
    Oh, and you were spot on, on the integration test part :)
  • 1
    @aaxa
    Ha ha.
    validateMail() function would be a great candidate for unit test ;) did I get it correct this time
  • 1
    @sbin If the method checks the validity of the content, then yes :)
  • 1
    @sbin Or if it validates an email address, then also yes :D
Add Comment