UI testing functions within test Class - swift

I am implementing UI testing and I have created a 900 lines test class. This file contains 16 different tests that I would like to have lest separately in the left pane so that I can run specific ones as required. However, when I include the 16 funcs there are no tests listed. When I comment out the 16 funcs, I can see (and run) all the tests as a single line.
Here is the structure of the 16 funcs.
import XCTest
class zeroCounts: XCTestCase {
override func setUp() {
super.setUp()
continueAfterFailure = false
XCUIApplication().launch()
}
override func tearDown() {
super.tearDown()
}
func tests() {
testsRun = 0
currentRow = 0
func case_0000() {
// Do stuff
}
func case_0001() {
// Do stuff
}
func case_0010() {
// Do stuff
}
func case_0011() {
// Do stuff
}
func case_0100() {
// Do stuff
}
func case_0101() {
// Do stuff
}
func case_0110() {
// Do stuff
}
func case_0111() {
// Do stuff
}
func case_1000() {
// Do stuff
}
func case_1001() {
// Do stuff
}
func case_1010() {
// Do stuff
}
func case_1011() {
// Do stuff
}
func case_1100() {
// Do stuff
}
func case_1101() {
// Do stuff
}
func case_1110() {
// Do stuff
}
func case_1111() {
// Do stuff
}
}

To fix this problem, rename your test functions to begin with test. The test runner will find those functions and show them in the navigation panel just like you want.
func test_1111() {
// Do stuff
}

Related

Swift AsyncStream variable, use after finish()

I'm currently reading about AsyncStreams and as I understand it, they are best for tasks that produce some results over time, but have a lifespan - e.g. start and end. Here's an example I am playing with:
struct AHardWorkingStruct {
lazy var updates = AsyncStream<String> { continuation in
onProgress = { value in
continuation.yield(value)
}
onFinish = { value in
continuation.yield(value)
continuation.finish()
}
}
private var onProgress: (String) -> Void = { _ in () }
private var onFinish: (String) -> Void = { _ in () }
func doSomeWork() async {
let numbers = (0..<20).map { anInt in
anInt^2
}
for number in numbers {
onProgress("The number is \(number)")
if(number == 20) {
onFinish("I'm DONE!")
}
}
}
}
I then have AHardWorkingStruct as a property in my view controller and use it like so:
class MyViewController: UIViewController {
var myHardWorker = AHardWorkingStruct()
#IBAction func tapToRun(_ sender: UIButton) {
Task {
await myHardWorker.doSomeWork()
}
}
override func viewDidLoad() {
super.viewDidLoad()
Task {
for await stream in myHardWorker.updates {
print(stream)
}
}
}
This works perfectly when I tap the button.
However, once I call finish() on the stream, I no longer get the updates from it - which I understand is expected behaviour.
My question is, how can I keep getting updates from a stream that is a variable on a long-lived object? Is there a "reset"? (other than getting rid of the lazy and nilling the variable)
I also have a 2nd part to this - what would be the best way to unit test an asyncstream? I've tried using expectations, but not sure if this is right.

ORSSerialPort with Arduino

I've been trying for a long time to program an Xcode interface to communicate with my Arduino Mega. but the whole thing didn't work as well as intended. I did the whole thing with ORSSerialPort.
In the Xcode project I wrote this for the swift file ViewController.swift :
import Cocoa
import ORSSerial
class ViewController: NSViewController, ORSSerialPortDelegate {
var serialPort = ORSSerialPort(path: "/dev/cu.usbmodem142101")
func SendString(data: String){
let stringData = Data(data.utf8)
serialPort?.send(stringData)
}
func openPort(){
serialPort?.baudRate=9600
serialPort?.delegate=self
serialPort?.parity = .none
serialPort?.numberOfStopBits = 1
serialPort?.open()
print("serialport is open")
}
func closePort(){
serialPort?.delegate=nil
serialPort?.close()
print("serialport is close")
}
override func viewDidLoad() {
super.viewDidLoad()
}
override var representedObject: Any? {
didSet {
}
}
#IBAction func onButton(_ sender: Any) {
openPort()
}
#IBAction func OffButton(_ sender: Any) {
closePort()
}
#IBAction func SendButton(_ sender: Any) {
SendString(data: "stringdata blablabla")
}
func serialPortWasOpened(_ serialPort: ORSSerialPort) {
print("serialPort to \(serialPort) is run")
}
func serialPortWasRemovedFromSystem(_ serialPort: ORSSerialPort) {
self.serialPort = nil
}
}
and this code i have load on the Arduino mega:
String angel;
void setup() {
Serial.begin(9600);
}
void loop() {
angel = Serial.readString();
Serial.println(angel);
delay(350);
}
unfortunately it doesn't work and I don't know why.
Your question doesn't provide any detail about what part(s) don't work, but there's one definite problem.
Your Arduino program looks like it echos everything it receives on the serial port back on the same port. In order to see that on the computer, you'll have to implement the serialPort(_:didReceive:) method in your view controller. Something like this:
func serialPort(_ serialPort: ORSSerialPort, didReceive data: Data) {
guard let string = String(data: data, encoding: .ascii) else { return; }
print("Received: \(string)")
}

TDD data from func not being returned in XCTest case Swift

This is what I have in my QuickAddViewController.swift file
let exercisesData = ExerciseDatabase()
var workoutTypesDictionary = Dictionary<String,Dictionary<String,Array<String>>>()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
workoutTypesDictionary = self.exercisesData.exercisesByWorkoutType
}
func getWorkoutTypes() -> [String] {
var workoutTypesArray : [String] = []
for workoutType in workoutTypesDictionary.keys {
workoutTypesArray.append(workoutType)
}
return workoutTypesArray
}
This is my QuickAddViewTest.swift file
class QuickAddViewTests: XCTestCase {
var quickAddViewController : QuickAddViewController!
override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
quickAddViewController = QuickAddViewController()
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}
func testGetWorkoutTypes() {
let workoutTypesArray = quickAddViewController.getWorkoutTypes()
let expectedArray = ["Weight Training", "Sports & Recreation", "Body Weight", "Cardio"]
print("Workout Array: \(workoutTypesArray)")
print("Expected Array: \(expectedArray)")
XCTAssertEqual(workoutTypesArray, expectedArray)
}
When I run the app and print getWorkoutTypes(), the function return the correct values. However, when I try to return the same values in testGetWorkoutTypes(), nothing gets returned and my test fails.
Add
quickAddViewController.loadViewIfNeeded()
This causes wires up storyboard connections (which makes other testing possible) and triggers a call back to viewDidLoad().

I need a list of AdMob Functions SWIFT

I've looked all over google's AdMob site, I just want a list of all the inbuilt functions I can use for banners and interstitials.
eg. adViewDidReceiveAd() interstitialDidDismissScreen()
If anyone knows where I can find this, please let me know.
This is bannerView functions where you have to implement GADBannerViewDelegate to be able to use it (don't forget to set the delegate for your banner as myBanner.delegate = self so they will be called).
func adViewDidReceiveAd(bannerView: GADBannerView!) {
}
func adViewDidDismissScreen(bannerView: GADBannerView!) {
}
func adViewWillDismissScreen(bannerView: GADBannerView!) {
}
func adViewWillPresentScreen(bannerView: GADBannerView!) {
}
func adViewWillLeaveApplication(bannerView: GADBannerView!) {
}
func adView(bannerView: GADBannerView!, didFailToReceiveAdWithError error: GADRequestError!) {
}
And for interstitial delegates you will need to implement GADInterstitialDelegate to able to use it.(don't forget to set the delegate for your interstitlal as interstitial.delegate = self so they will be called).
func interstitialDidReceiveAd(ad: GADInterstitial!) {
}
func interstitialDidDismissScreen(ad: GADInterstitial!) {
}
func interstitialWillDismissScreen(ad: GADInterstitial!) {
}
func interstitialWillPresentScreen(ad: GADInterstitial!) {
}
func interstitialWillLeaveApplication(ad: GADInterstitial!) {
}
func interstitial(ad: GADInterstitial!, didFailToReceiveAdWithError error: GADRequestError!) {
}
Implement these after class declaration, ex: class test:GADInterstitialDelegate {}

Wait for completion before executing next step

I have a few processes that need to be completed in order when my TableView loads. I would expect that it would wait until the code completed before executing the next line of code, but it seems that's not the case. Is there a way to have these wait until completion before executing the next step?
override func viewDidLoad() {
super.viewDidLoad()
performTask1()
performTask2()
performTask3()
}
Thanks for all the help!
The typical example to make each of these methods take a completionHandler parameter, e.g.:
func perform1(completionHandler: () -> Void) {
doSomethingAsynchronously() {
completionHandler()
}
}
func perform2(completionHandler: () -> Void) {
doSomethingElseAsynchronously() {
completionHandler()
}
}
func perform3(completionHandler: () -> Void) {
doSomethingCompletelyDifferentAsynchronously() {
completionHandler()
}
}
Then you can run them like so:
override func viewDidLoad() {
super.viewDidLoad()
perform1 {
self.perform2 {
self.perform3 {
}
}
}
}