pytest

pytest的执行方式及搜索原则

萝らか妹 提交于 2019-11-30 19:20:11
pytest的执行方式 Pytest/py.test(终端,命令行,pycharm可配置pytest方式执行) 1. Pytest –v (最高级别信息—verbose) 2. pytest -v -s filename(输出打印) 3.Pytest-q (静默) 搜索用例原则 1.pytest将在当前目录及其子目录中运行test _ * .py或* _test.py形 式的所有文件。 2.以test_开头的函数,以Test开头的类,以test_开头的方法。所有包 package都要有__init__.py文件。 3.Pytest可以执行unittest框架写的用例和方法 来源: https://www.cnblogs.com/QaStudy/p/11637191.html

pytest框架结构运行规则及命名方式

可紊 提交于 2019-11-30 19:15:21
import pytestdef setup_module(): print('整个模块.py开始')def teardown_module(): print('整个模块的.py结束')def setup_function(): print('不在类中的函数前')def teardown_function(): print('不在类中的函数后')def test_w_one(): print('不在类中的方法1')def test_w_two(): print('不在类中的方法2')class TestClass: def setup_class(self): print('类前面') def teardown_class(self): print('类之后') def setup_method(self): print('方法前') def teardown_method(self): print('方法后') def test_one(self): x='this' assert 'h' in x def test_two(self): x='hello' assert 'h4'==x def test_three(self): a='hello' b='hello world' assert a in bif __name__ == '__main__': pytest

python 自动化测试框架unittest与pytest的区别

天大地大妈咪最大 提交于 2019-11-30 18:38:25
前言: 有使用过unittest单元测试框架,再使用pytest单元测试框架,就可以明显感觉到pytest比unittest真的简洁、方便很多。 unittest与pytest的区别: 主要从用例编写规则、用例的前置和后置、参数化、断言、用例执行、失败重运行和报告这几个方面比较unittest和pytest的区别 来源: https://www.cnblogs.com/cuitang/p/11635103.html

Chrome 59 support for basic auth credentials in URLs alternative for usage with Chromedriver?

China☆狼群 提交于 2019-11-30 18:23:54
问题 With Chrome 59 the support for putting basic auth credentials in URLs - like https://foo:bar@www.foo.com has ended - this was warned a while ago within https://www.chromestatus.com/feature/5669008342777856. Has anyone had to work around this with Selenium and Chromedriver yet? Specifically within Python? 回答1: In our situation (automated testing using WebDriver via C# with NTLM auth) we found that once you hit the page with the credentials although you can't load the sub-resources on the page

writing a pytest function to check outputting to a file in python?

白昼怎懂夜的黑 提交于 2019-11-30 17:42:11
I asked this question about how to write a pytest to check output in stdout and got a solution. Now I need to write a test case , to check if the contents are written to the file and that the contents are written as expected eg: def writetoafile(): file = open("output.txt",w) file.write("hello\n") file.write("world\n") file.close() now a pytest function to check if it written: def test_writeToFile(): file = open("ouput.txt",'r') expected = "hello\nworld\n" assert expected==file.read() while this seems to work, I do not think this is ideal, especially hard-coding. how are these kind of test

How to accumulate state across tests in py.test

寵の児 提交于 2019-11-30 17:29:38
问题 I currently have a project and tests similar to these. class mylib: @classmethod def get_a(cls): return 'a' @classmethod def convert_a_to_b(cls, a): return 'b' @classmethod def works_with(cls, a, b): return True class TestMyStuff(object): def test_first(self): self.a = mylib.get_a() def test_conversion(self): self.b = mylib.convert_a_to_b(self.a) def test_a_works_with_b(self): assert mylib.works_with(self.a, self.b) With py.test 0.9.2, these tests (or similar ones) pass. With later versions

How do I Pytest a project using PEP 420 namespace packages?

走远了吗. 提交于 2019-11-30 17:14:13
问题 I am trying to use Pytest to test a largish (~100k LOC, 1k files) project and there are several other similar projects for which I'd like to do the same, eventually. This is not a standard Python package; it's part of a heavily customized system that I have little power to change, at least in the near term. Test modules are integrated with the code, rather than being in a separate directory, and that is important to us. The configuration is fairly similar to this question, and my answer there

Is there a way to change the location of pytest's .cache directory?

筅森魡賤 提交于 2019-11-30 14:37:21
问题 I need to be able to change the location of pytest's .cache directory to the env variable, WORKSPACE. Due to server permissions out of my control, I am running into this error because my user does not have permission to write in the directory where the tests are being run from: py.error.EACCES: [Permission denied]: open('/path/to/restricted/directory/tests/.cache/v/cache/lastfailed', 'w') Is there a way to set the path of the .cache directory to the environment variable WORKSPACE? 回答1: You

10、fixture参数化

放肆的年华 提交于 2019-11-30 14:29:40
fixture的参数可以解决大量重复代码工作,比如数据库的连接、查询、关闭等.同样可以使用参数化来测试多条数据用例 例一、 import pytest @pytest.fixture(params=[("redis", "6379"), ("elasticsearch", "9200")]) def params(request): return request.param @pytest.fixture() def db(params): print(f"\nuse to connection: {params}") yield print(f"\nclose to connection:{params}") @pytest.mark.usefixtures("db") def test_demo(): assert 1 == 1 运行结果: ╰ pytest -v -s test_data.py ======================================================= test session starts ======================================================== platform darwin -- Python 3.7.4, pytest-4.4.0, py-1.8.0,

09、自动执行

大兔子大兔子 提交于 2019-11-30 14:26:53
除了使用pytest.fixture.userfixtures和传参两种显式的调用fixture之外.也可以自动的执行fixture,在定义fixture的时候设置autouser=True即可 from datetime import datetime from time import sleep import pytest @pytest.fixture(autouse=True) def timer_session_scope(): start = datetime.now() print(f"\nstart:{start}") yield end = datetime.now() print(f"total time cost:{end - start}") def test_timer(): sleep(1.3) assert 2 == 2 运行结果: ╰ pytest -v -s test_data.py ======================================================= test session starts ======================================================== platform darwin -- Python 3.7.4, pytest-4.4.0, py-1