[英]How to test Webview is loaded or not without staticTexts in XCTest in swift
[英]How to test imported custom font in XCTest (Swift 5)
大家好! 我在項目中導入了自定義字體。 如何使用文本字段中的文本對其進行測試? 例如,我有文本字段 =“堆棧溢出”,字體 = UIFont(名稱:“Roboto-Regular”,大小:17)。 如何檢查 text = "Stack overflow" 是否有 font = UIFont(name: "Roboto-Regular", size: 17)? 你有什么想法,如何在 XCTest 中實現它?
擴展
import UIKit
extension UIFont {
static func robotoRegular (size: CGFloat) -> UIFont? {
return UIFont(name: "Roboto-Regular", size: size)
}
static func robotoBold (size: CGFloat) -> UIFont? {
return UIFont(name: "Roboto-Bold", size: size)
}
}
您是否會對使用 fonts 的 class 進行單元測試?
我建議你有一個自定義的 label 子類,你希望它是那個字體。 class 和測試如下所示:
class CustomLabelWithFont: UILabel {
override init(frame: CGRect) {
super.init(frame: frame)
setStyle()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func setStyle() {
self.font = .robotoBold(size: 20)
}
}
測試文件看起來像這樣
class TestTestTests: XCTestCase {
func testFontSetting() throws {
let customLabel = CustomLabelWithFont(frame: .zero)
XCTAssertEqual(customLabel.font, .robotoBold(size: 20))
XCTAssertNotEqual(customLabel.font, .systemFont(ofSize: 20))
}
}
如果你願意,你也可以直接測試你的 UIFont 擴展。 除了檢查沒有人意外更改了字體邏輯之外,這不會有太多實用性
func testFontItself() throws {
let expectedFont: UIFont = .robotoBold(size: 20)!
XCTAssertEqual(expectedFont, UIFont(name: "Roboto-Regular", size: 20))
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.