小编pr4*_*4sh的帖子

如何检测pytest测试用例何时失败?

我正在使用pytest和selenium来自动化网站.我想在测试用例失败时才拍摄一些屏幕截图.我已经普遍使用了TestNG,而使用TestNG,它使用了ITestListner.我们在pytest中有类似的东西吗?

我试图使用teardown_method()来实现这一点但是当测试用例失败时,这个方法没有被执行.

import sys

from unittestzero import Assert
class TestPY:
    def setup_method(self, method):
        print("in setup method")
        print("executing " + method.__name__)

    def teardown_method(self, method):
        print(".....teardown")
        if sys.exc_info()[0]:
            test_method_name = method
            print test_method_name

    def test_failtest(self):
        Assert.fail("failed test")
Run Code Online (Sandbox Code Playgroud)

teardown_method() 只有在没有失败时才会执行

python selenium pytest

13
推荐指数
2
解决办法
5637
查看次数

如何从 pytest.ini 读取自定义配置?

我想在 pytest.ini 文件上提供自定义参数并从代码中读取它。

[pytest]
markers =
    regression: mark a test as regression.
    sanity: mark a test as sanity.
    critical: mark a test as critical.
addopts= -sv --html=report.html
custom_value= test
Run Code Online (Sandbox Code Playgroud)

在这里,我想阅读 我在下面尝试过的custom_value但它不起作用并抛出ValueError: no option named 'custom_value'

def test_failtest(self, request):
    config = request.config
    tcv = config.getoption('custom_value')
    print "tcv->" + tcv
Run Code Online (Sandbox Code Playgroud)

python pytest

5
推荐指数
1
解决办法
4046
查看次数

如何通过selenium脚本获取移动原生应用程序中元素的页面源?

我正在使用 Appium 实现移动应用程序的自动化,我想要的只是本机应用程序元素的页面源。我在谷歌上搜索了很多,发现它可以用于网页,但没有找到任何针对移动设备本机应用程序的解决方案。就像下面的例子一样。

elem.getAttribute("innerHTML");
Run Code Online (Sandbox Code Playgroud)

或者

WebElement element = driver.findElement(By.id("foo"));
String contents = (String)((JavascriptExecutor)driver).executeScript("return arguments[0].innerHTML;", element);
Run Code Online (Sandbox Code Playgroud)

selenium automation appium

3
推荐指数
1
解决办法
1万
查看次数

如何计算用pytest编写的测试用例?

我的目标是获得包装/文件夹中的测试方法数量。我可以通过执行

py.test <folder> --collect-only|grep collected

这显示测试计数为

collected 104 items

但是,这将对参数化测试进行多次计数,例如,如果某个方法具有两组参数,则一次测试将被计数2次。有什么方法可以告诉pytest将它们视为单个?

command-line file-search pytest

3
推荐指数
3
解决办法
1839
查看次数