python - Selenium 测试 - 无论如何都会打印所有内容

标签 python testing selenium printing unit-testing

这听起来像是一个非常愚蠢的问题,但它是:

我正在用 python 编写 selenium 测试,基本上我的基本测试类如下所示:

chromedriver = "../selenium-tests/chromedriver"
os.environ["webdriver.chrome.driver"] = chromedriver

class TestsFoo(unittest.TestCase):
    base_url = None
    language = None

    def setUp(self):
        self.driver = webdriver.Chrome(chromedriver)
        self.verificationErrors = []
        self.accept_next_alert = True
        self.driver.set_window_size(1100, 800)

然后我的所有其他测试类都从该类扩展。示例:

class TestsFooChild(TestsFoo):
    def test_something(self):
        driver = self.driver
        driver.get("{}{}/myurl.html".format(
            self.base_url,
            self.language)
        )
        # do stuff

        print driver.current_url
        self.assertTrue(somethingTrue)

    def tearDown(self):
        self.driver.close()
        self.driver.quit()

languageurl 是这样定义的:

if __name__ == "__main__":
    TestsFoo.base_url = os.environ.get('URL')
    TestsFoo.language = os.environ.get('LANGUAGE')
    unittest.main()

对于一个非常非常小的问题,这是很多信息: 在进行单元测试时,如果你在某处打印了一个print,它只会在测试失败时被打印出来。

那么为什么在我的例子中,无论测试结果如何,每个 print 都会被打印出来?我只想在测试失败时打印我的 current_url

此外,我只是在研究它为什么会那样做。我正在积极寻找一种仅在测试失败时打印它的方法。所以这部分得到了照顾。但我很好奇..

最佳答案

我几乎从不单独使用 unittest,但据我所知,print 结果输出到控制台是 的默认行为单元测试。这是一个示例文件:

import unittest

class Test(unittest.TestCase):

    def test_one(self):
        print "Foo"

if __name__ == "__main__":
    unittest.main()

如果您将它保存为 test.py 并使用 python test.py 运行它,您将在控制台上打印出 Foo .

但是,如果你像这样用 nose 运行它Foo 在控制台上。您必须使用 -s--nocapture 运行它才能看到 Foo

关于python - Selenium 测试 - 无论如何都会打印所有内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28707077/

相关文章:

javascript - 如何在 JavaScript 中补偿 Nginx url 重写?

ruby - 如何 stub 方法调用,然后进行哈希查找?

java - Selenium ChromeDriver 与隐身模式下的 AdBlock

使用列表字典查找和替换的 Pythonic 方式

python - 我在 App Engine 上的谷歌云端点中收到 404

java - Java 项目中的 Android 类

python - 类型错误 : 'WebElement' object is not iterable

python selenium 不支持 firefox - 消息 : Can't load the profile. Profile Dir

Pythonic/有效的方法从每个包含字符串对象的 Pandas 数据框单元格中去除空格

rest - 如何以逗号分隔的方式获取 Jmeter 变量的值