Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
coolq48207y@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? -
sbin20117y@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 -
coolq48207y@sbin @nahson
Thank you both for explaining!
I need to get writing these ASAP.
It sounds quite clever actually! -
aaxa22217y@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 :) -
aaxa22217y@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)
-
coolq48207y@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 😄 -
aaxa22217y@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?
Related Rants
Not writing unit tests :(
rant
wk78