py.test在哪里以及如何寻找灯具?我在同一文件夹中的2个文件中有相同的代码.当我删除conftest.py时,找不到运行test_conf.py的cmdopt(也在同一个文件夹中.为什么没有搜索到sonoftest.py?
# content of test_sample.py
def test_answer(cmdopt):
if cmdopt == "type1":
print ("first")
elif cmdopt == "type2":
print ("second")
assert 0 # to see what was printed
Run Code Online (Sandbox Code Playgroud)
import pytest
def pytest_addoption(parser):
parser.addoption("--cmdopt", action="store", default="type1",
help="my option: type1 or type2")
@pytest.fixture
def cmdopt(request):
return request.config.getoption("--cmdopt")
Run Code Online (Sandbox Code Playgroud)
import pytest
def pytest_addoption(parser):
parser.addoption("--cmdopt", action="store", default="type1",
help="my option: type1 or type2")
@pytest.fixture
def cmdopt(request):
return request.config.getoption("--cmdopt")
Run Code Online (Sandbox Code Playgroud)
文档说
http://pytest.org/latest/fixture.html#fixture-function
- pytest因test_前缀而找到test_ehlo.测试函数需要一个名为smtp的函数参数.通过查找名为smtp的夹具标记函数来发现匹配夹具功能.
- 调用smtp()来创建实例.
- 调用test_ehlo()并在测试函数的最后一行失败.
是否可以从Symfony2/Doctrine中的现有数据库生成灯具?我怎么能这样做?
例:
我已经定义了15个实体,我的symfony2应用程序正在运行.现在有些人可以浏览到该应用程序,并且使用它已经插入了大约5000行,直到现在.现在我希望插入的东西作为固定装置,但我不想手工完成.如何从数据库生成它们?
在单元测试中,setup方法用于创建测试所需的对象.
在那些设置方法中,我喜欢使用断言:我知道我想在这些对象中看到什么值,我喜欢通过断言来记录这些知识.
在最近的文章中对单元测试调用其他单元测试在这里计算器,总的感觉似乎是,单元测试应该没有调用其他检查:这个问题的答案似乎是,你应该重构你的设置,使测试用例做不相互依赖.
但是"设置与断言"和单元测试调用其他单元测试没有太大区别.
因此我的问题是:在设置方法中使用断言是一种好习惯吗?
编辑:
答案结果证明:这不是一般的好习惯.如果需要测试设置结果,建议在断言中添加单独的测试方法(我勾选的答案); 为了记录意图,请考虑使用Java断言.
我是新手使用RSpec在使用MySQL数据库的Rails应用程序中编写测试.我已经定义了我的灯具,并在我的规格中加载它们如下:
before(:all) do
fixtures :student
end
Run Code Online (Sandbox Code Playgroud)
这个声明是否在学生表中保存我的灯具中定义的数据,或者只是在测试运行时将数据加载到表中,并在运行所有测试后将其从表中删除?
我正在阅读本指南中关于Rails装置的信息(谢谢,trevorturk).看来你在Yaml文件中定义了类,它们会自动加载到test
DB中 - 很酷.
但是如果你想指定这个食谱属于那本食谱(或其他什么)你怎么做?
您是否应该在Yaml代码中指定cookbook.id
和recipe.cookbook_id
手动赋值?(只是一个猜测 - 指南没有显示那样的东西.)或者是否有更合适的方式?
嗨,我想对使用Devise和CanCan的Rails 3应用程序进行一些功能测试.
在我的用户模型中,我有用户年龄,我想测试用户只能访问某个页面,如果他们是:
我在Devise文档中看到我可以使用:*sign_in*并且我把它放在我的测试中它似乎工作 - 测试没有错误,因为我有:
include Devise::TestHelpers
Run Code Online (Sandbox Code Playgroud)
在我的*test_helper.rb*
当我拿出它时,我的测试会出错,因为*sign_in*没有定义.所以这不是帮手问题.
当我运行测试并检查span#loggedin是否有一次出现时,测试失败,因为有0次出现.span#loggedin仅出现*if user_signed_in?*
我需要在我的灯具或测试中创建一个也是完全注册用户(已确认等)的测试用户?
查看代码:
<% if user_signed_in? %>
<span id="loggedin">User is signed in</span>
User age is <span id="age"><%= current_user.age.to_s %></span>
<% end %>
Run Code Online (Sandbox Code Playgroud)
测试代码:
test "should get index" do
sign_in :one
get :index
assert_response :success
assert_select 'span#loggedin', :count => 1
end
Run Code Online (Sandbox Code Playgroud)
夹具:
one:
email: jez@example.com
age: 36
Run Code Online (Sandbox Code Playgroud)
当我手动登录时,它在开发中工作正常,但我希望自动化它 - 真正的测试点!
所以,我正在尝试在rails项目的上下文中学习rspec BDD测试框架.我遇到的问题是,对于我的生活,我不能在rspec描述中正确加载我的灯具.
免责声明:是的,有比使用固定装置更好的东西.在尝试使用工厂女孩,摩卡,自动测试等相关工具之前,我一次尝试学习一件事(特别是rspec).因此,我试图让死者变得简单,如果笨重,固定装置工作.
无论如何,这是代码:
/test/fixtures/users.yml -
# password: "secret"
foo:
username: foo
email: foo@example.com
password_hash: 3488f5f7efecab14b91eb96169e5e1ee518a569f
password_salt: bef65e058905c379436d80d1a32e7374b139e7b0
bar:
username: bar
email: bar@example.com
password_hash: 3488f5f7efecab14b91eb96169e5e1ee518a569f
password_salt: bef65e058905c379436d80d1a32e7374b139e7b0
Run Code Online (Sandbox Code Playgroud)
/spec/controllers/pages_controller_spec.rb -
require 'spec/spec_helper'
describe PagesController do
integrate_views
fixtures :users
it "should render index template on index call when logged in" do
session[:user_id] = user(:foo).id
get 'index'
response.should render_template('index')
end
end
Run Code Online (Sandbox Code Playgroud)
当我运行'rake spec'时我得到的是:
NoMethodError in 'PagesController should render index template on index call when logged in'
undefined method `user' for #<Spec::Rails::Example::ControllerExampleGroup::Subclass_1:0x2405a7c>
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.5/lib/action_controller/test_process.rb:511:in `method_missing' …
Run Code Online (Sandbox Code Playgroud) 更新3:看起来这是特定于.yml.erb中的灯具 - 即使我没有模板化代码,似乎yml.erb文件中的灯具也没有加载.有一个简单的.yml文件.这可能与设计本身无关.
注意:请参阅更新3注释以了解相关更改
我需要在我的rails应用程序中生成Devise用户.我注意到清除数据库和加载灯具会加载除Devise用户之外的所有其他灯具(Update 3:在.yml.erb文件中).
我已经看到了这个其他线程,但我尝试了所有选项,但似乎仍然没有加载灯具.
# ../fixtures/users.yml.erb
user1:
email: user1@mysite.com
name: user1
encrypted_password: <%= Devise.bcrypt(User, 'passw0rd!') %>
# also tried encrypted_password: User.new(password_salt: '$2a$10$PoBe1MvkoGJsjMVTEjKqge').send(:password_digest, 'somepassword')
admin: true
Run Code Online (Sandbox Code Playgroud)
从控制台:
要清除测试db:
$ bundle exec rake db:schema:load RAILS_ENV=test
Run Code Online (Sandbox Code Playgroud)
要将灯具加载到测试db中:
$ bundle exec rake db:fixtures:load RAILS_ENV=test
Run Code Online (Sandbox Code Playgroud)
在测试中运行rails console(未找到用户,但正在加载其他模型装置,如App):
$ rails c test
Loading test environment (Rails 4.1.5)
irb(main):001:0> User.first
User Load (0.1ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 1
=> nil
irb(main):002:0> App.first
App Load …
Run Code Online (Sandbox Code Playgroud) 我有一个使用灯具的功能测试.我也在我的单元测试中使用灯具,但它们没有缺陷.运行功能测试时,我得到一个:
NoMethodError: undefined method 'recycle!' for #<Response:0x10346be10>
/test/functional/responses_controller_test.rb:10:in 'test_testing'
到目前为止,我的功能测试只是进行索引操作.例:
setup do
@response = responses(:one)
end
test "testing" do
get :index
assert true
end
Run Code Online (Sandbox Code Playgroud)
我的TestHelper类确实包含了所有灯具,因此响应灯具肯定会加载.就像我说的那样,灯具在单元测试中运行得非常好.
知道可能导致这种情况的原因吗?
我正在使用Symfony 3.4.0,我尝试使用以下命令加载灯具:
php bin/console doctrine:fixtures:load
Run Code Online (Sandbox Code Playgroud)
创建数据时出错,出了什么问题?
fixtures ×10
rspec ×2
ruby ×2
symfony ×2
testing ×2
unit-testing ×2
assertions ×1
bdd ×1
devise ×1
doctrine ×1
doctrine-orm ×1
erb ×1
pytest ×1
python ×1
recycle ×1
refactoring ×1
symfony-3.4 ×1
tdd ×1