Updated 30 July 2021
Unit testing in swift iOS is the way to test our code and reduce manual testing.
And the Unit test is a function that invoked our code.
Unit testing reduces the bug in our code and finds memory leaks.
The Unit testing in iOS uses the XCTest framework to perform our unit testing.
Use the XCTest framework to write unit tests for your Xcode projects that integrate seamlessly with Xcode’s testing workflow.
Unit tests have their own target in Xcode.
A subclass of XCTestCase
contains the testing methods.
A unit test reduces the cost of changes.
1-> When we creating the project then we need to check the include test checkbox.
2-> And for old projects follow the below steps to add tests.
3-> Create the class and put code which you want the test
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
class TestEvenOddNumber { var number = 0 func checkEvenNumber(numberValue: Int) -> Bool { number = numberValue if self.number % 2 == 0 { print("\(self.number) is even number") return true } return false } func checkOddNumber(numberValue: Int) -> Bool { number = numberValue if !(self.number % 2 == 0) { print("\(self.number) is even number") return true } return false } } |
4-> Now we will create the object of TestEvenOddNumber in our Unit test class
1 |
var checkNumberObj: TestEvenOddNumber! |
5-> Now we will instantiate the TestEvenOddNumber in setUp Function
1 2 3 4 |
override func setUpWithError() throws { checkNumberObj = TestEvenOddNumber() } |
6-> Now we will create the test function to test our code.
1 2 3 4 5 6 |
func testEvenNumber() throws { XCTAssert(checkNumberObj.checkEvenNumber(numberValue: 34)) } func testOddNumber() throws { XCTAssert(checkNumberObj.checkOddNumber(numberValue: 34)) } |
7-> Now we will override the tearDown function to clear the object after finish the test
1 2 3 |
override func tearDownWithError() throws { checkNumberObj = nil } |
8-> Now we will click on the test function run button to check our code
when we test even number for 34 then test success.
but with the same number when we test odd number test case then result come failed.
And thanks for reading this blog, for detail info please click here
For more blogs please click here.
So pls follow the above step and And if you have any issues or suggestions you can leave your message in the comment section I will try to solve this.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.