pytest

pytest-fixture

♀尐吖头ヾ 提交于 2019-12-11 18:29:52
fixture的作用 1.同unittest的setup和teardown,作为测试前后的初始化设置。 fixture的使用 1.作为前置条件使用 2.fixture的的作用范围 1.作为前置条件使用 @pytest.fixture() def a(): return 3 def test_b(a): assert a==3 2.fixture的作用范围 首先实例化更高范围的fixture.默认为scope="function",每个测试函数都会执行一次。 session : 多个.py文件执行时,只调用一次 module: 每一个.py调用一次 class : 每个类调用一次 function: 每个函数调用一次 3.fixture 作为setup/teardown执行 yield 前的在测试之前执行,yield后的在测试完后执行 @pytest.fixture def a(): print("setup") yield print("teardown") def test_b(a): print("测试") 4.fixture with # @pytest.fixture() # def write_file(): # f = open("myfile.txt","w") # f.write("hello") # yield # f.close() @pytest

pytest实际编码中特殊问题的解决

微笑、不失礼 提交于 2019-12-11 17:50:01
1.一个用例中想要写多个断言 现状:如果用assert的话,第一个assert失败后,后面的代码就不执行了 优化:想要前一个断言失败后后续断言继续执行的话,需要安装插件 pip install pytest-assume 代码实例:后续补充 2.测试用例的执行顺序 现状:文件名称 按 ASCII 码排序;文件内容,按代码先后顺序排序 优化:想要自己控制测试用例的执行顺序,需要安装插件 pip install pytest-ordering 代码实例:后续补充 3.用例间的依赖问题 现状:如增删改类型的测试用例,如果增加失败了,那么继续执行删改,则必然是失败的 优化:想要增测试用例执行成功后才去执行删改的测试用例,如果增失败则删改的测试用例不执行,需要安装插件 pip install pytest -dependency,使用该插件可以标记一个test作为其他test的依赖,当依赖项执行失败时,那些依赖它的test将会被跳过。 d 来源: https://www.cnblogs.com/wang-mengmeng/p/12024005.html

Using local imports in Pytest

无人久伴 提交于 2019-12-11 16:57:12
问题 I have never really fully understood how packages are handled in Python and I'm having a problem with that right now. But googling doesn't seem to help as I find the topic really confusing. I have a project with this structure: project_name/ src/ main.py utils/ string_utils.py tests/ test_string_utils.py I am using Pytest for running unit testing and currently inside the "test_string_utils.py" file I have the following: from ..src.utils.string_utils import StringUtilsClass But I go to the

A way to add test specific params to each test using pytest

爷,独闯天下 提交于 2019-12-11 16:15:17
问题 I'm working on automated testing in pytest, and i'm looking for a way to read params from a config file that are specific to a test and add it to the appropriate test. for example I would like my config.ini file to look like this: [Driver] #some genral variables [Test_exmpl1] #variables that I would like to use in Test_exmpl1 username= exmp@gmail.com password= 123456 [Test_exmpl2] #variables that I would like to use in Test_exmpl2 username= exmp2@gmail.com password= 123456789 Now in the code

How to show the break point instead of assertion error while running the test case in pytest

谁说我不能喝 提交于 2019-12-11 15:05:07
问题 I am using pytest to validate db data. I am generating a html report which shows test case result,For failed case it only shows Assertion error, But i need the break point where the test case fails. Can anyone help with this? Main_methods.py import pymongo import re import unittest import pytest class Main_methods(): def minlength(self,data,category_name,min_length): ''' validate the minimum length condition by comparing with db data for a given category Parameter: Db data, category name,

monkeypatching not carrying through class import

非 Y 不嫁゛ 提交于 2019-12-11 12:46:15
问题 I'm trying to test some code using pytest and need to change a function from some module. One of my imports also imports that function, but this is failing when I change the method using monkeypatch . Here is what I have: util.py def foo(): raise ConnectionError # simulate an error return 'bar' something.py from proj import util need_this = util.foo() print(need_this) test_this.py import pytest @pytest.fixture(autouse=True) def fix_foo(monkeypatch): monkeypatch.setattr('proj.something.util

How to separate tests and fixtures

不打扰是莪最后的温柔 提交于 2019-12-11 11:56:40
问题 I have example project in PyCharm - consist of a simple test that checking sign-in to the given correct Slack workspace. It has web_drivers directory with chromedriver inside, conftest.py with webdriver setup for test and tests.py with actual test, f.e. conftest.py import os import pytest from selenium import webdriver @pytest.fixture(scope='class') def driver_get(request): web_driver = webdriver.Chrome(executable_path=os.path.join("web_drivers","chromedriver.exe")) yield web_driver def fin()

TravisCI with pytest and numpy.load(): File not found

…衆ロ難τιáo~ 提交于 2019-12-11 11:01:49
问题 I have the following in my tests/conftest.py file, used to load a numpy array as a fixture object for tests that are run by pytest: @pytest.fixture(scope="module") def my_fixture(): return np.load(os.path.join(os.getcwd(), "fixture", "example.npy") The file projectname/tests/fixture/example.npy is present. This runs fine when I launch my tests from PyCharm, but when the tests run on TravisCI I get a file not found error, with the file path shown as missing the tests directory, i.e. it should

Py.Test parametrizing based on parametrized fixture

余生颓废 提交于 2019-12-11 10:22:52
问题 I have a class scoped parametrized fixture that gets 3 databases for its params and returns a connection to each one. Tests in a class uses this fixture to test each DB connection attributes. Now I have a new class with database tables tests that I want to use the above fixture but to be parametrized on each connection tables. Any suggestion on the pytest way to implement this? I can't find a way to parametrize based on an already parametrized element. Thanks 回答1: Test classes are used to:

Getting error TypeError: 'module' object has no attribute '__getitem__'

一笑奈何 提交于 2019-12-11 10:03:24
问题 I am trying to write py.test based test cases. my test.py is !flask/bin/python import pytest import config @pytest.fixture def app(request): SQLALCHEMY_DATABASE_URI = 'postgresql://sanjeev:sanjeev@localhost:5432/app' config[SQLALCHEMY_DATABASE_URI] db.create_all() def fin(): db.session.remove() db.drop_all() request.addfinalizer(fin) def test_foo(app): pass My config.py file looks like import os from sqlalchemy.ext.declarative import declarative_base Base = declarative_base() basedir = os