# sample_test.rb
gem 'minitest'
require 'minitest/autorun'
module Minitest
def self.plugin_bogus_options(opts, options)
end
def self.plugin_bogus_init(options)
p "Writing first pluging"
end
end
class SomeTest < Minitest::Test
def test_that_it_passes
assert_equal true, true
end
end
When I execute ruby sample_test.rb it doesn't print anything. Where I went wrong. Could you help me.
Your bogus plugin needs to be in a file at the following path:
minitest/bogus_plugin.rb:
Minitest will scan for files that match the minitest/*_plugin.rb patterns and activate them.
If you still struggling with that and for anyone with this problem, I found the answer here : http://www.samuelmullen.com/2013/11/extending-minitest-5-progress-reporters/
"In the ::load_plugins method in the minitest.rb file of Minitest, it uses Gem::find_files to search for plugins. The ::find_files method uses the $LOAD_PATH global variable to determine which directories in which to look. So, in order for Minitest to find your plugins, you’ll either need to create it as a Gem, or push your directory onto the $LOAD_PATH array."
Related
I'm just basically asking:
if it's considered OK to use exec() in this context
if there's a better/more pythonic solution
for any input or comments on how my code could be improved
First, some context. I have main.py which basically takes input and checks to see if I've written a command. Let's say I type '/help'. The slash just tells it my input was supposed to be a command, so then it checks if a function called 'help' exists, and if so, that function will be run.
To keep things tidy in main.py, and to allow myself to add more commands easily, I have a 'commands' directory, with individual command files in it, such as help.py. help.py would look like this for example:
def help():
print("You've been helped")
So then of course, I need to import help() from help.py, which was trivial.
As I added more commands, I decided to add an init.py file where I'd keep all the command import lines of code, and then just do 'from init import *' in main.py. At first, each time I added a command, I'd add another line in init.py to import it. But that wasn't as flexible as I wanted, so I thought, there's got to be a way to just loop through all the .py files in my commands directory and import them. I struggled with this for a while but came up with a solution that works.
In the init.py snippet below, I loop through the commands directory (and a couple others, but they're irrelevant to the question), and you'll see I use the dreaded exec() function to actually import the commands.
loaded, failed = '', ''
for directory in command_directories:
command_list = os.listdir(directory)
command_list.sort()
for command_file in command_list:
if command_file.endswith(".py"):
command_name = command_file.split(".")[0]
try:
# Evil exec() hack to use variable-name directories/modules
# Haven't found a more... pythonic... way to do this
exec(f"from {directory}.{command_name} import {command_name}")
loaded = loaded + f" - Loaded: {command_name}\n"
except:
failed = failed + f" - Failed to load: {command_name}\n"
if debug == True:
for init_debug in [loaded, failed]: print(init_debug)
I use exec() because I don't know a better way to make a variable with the name of the function being loaded, so I use {command_name} in my exec string to arbitrarily evaluate the variable name that will store the function I'm importing. And... well, it works. The functions work perfectly when called from main.py, so I believe they are being imported in the correct namespace.
Obviously, exec() can be dangerous, but I'm not taking any user input into it, just file names. Filenames that I only I am creating. This program isn't being distributed, but if it was, then I believe using exec() would be bad since there's potential someone could exploit it.
If I'm wrong about something, I'd love to hear about it and get suggestions for a better implementation. Python has been very easy to pick up for me, but I'm probably missing some of the fundamentals.
I should note, I'm running python 3.10 on replit (until I move this project to another host).
I am using Sphinx to build documentation for a package. I can't find a way to skip (remove) empty submodules when building doc. Example in Section 2 there is a subsection named Submodule which is blank I want to skip such subsections. I tried using the following code.
def skip_util_classes(app, what, name, obj, skip, options):
if what == "Submodules":
skip = True
return skip
def setup(sphinx):
sphinx.connect("autodoc-skip-member", skip_util_classes)
The above code is not eliminating section Submodules.
I want to know how can I define a section or subsection that needs to be skipped? And how can I define empty section or subsection that I want to skip in skip_util_classes?
Something like the skip_util_classes function (a handler for the autodoc-skip-member event) is used when you want Sphinx to ignore certain members of Python modules or classes. See Connect Sphinx autodoc-skip-member to my function.
This technique cannot be used to skip or remove sections in generated .rst files. Here are two suggestions that might help with that problem:
Create a custom sphinx-apidoc template. See Remove the word "module" from Sphinx documentation.
Don't run sphinx-apidoc over and over again. Run the tool once, tweak the output and add it to source control. See Keeping the API updated in Sphinx.
Is it possible to load the step definitions I have defined into the calabash-android console?
I would like to be able to use them when navigating the app within the console.
Thanks
No from the console you can not run a single step definition.
But you can start execution of a test at a specific line appending parameter to the call to start your test
:<linenumber>
This will start execution of your feature file from that specific line and it will run from there to the end of the file.
So while it is not what you are looking for at least it is something.
Did you try step('<step_name>') method?
To be honest I'm not sure if this will work. I know it's working insinde Ruby methods and step definitions - I wanted to post a comment but I can't with 28 points of reputation ;)
You can also try making ruby methods with code from within the step definition:
Then /^I do something$/ do
some code
goes here
end
def do_something
some code
goes here
# same code as in step definition
end
or just use step method:
def do_something
step('I do something')
end
and then call it in a calabash console (I prefer using binding.pry inside some script rather than calling "pure" calabash-console - it makes me sure that I will have all needed methods included).
Rubymine returns an alert of "Undefined step reference" for the two steps of this scenario. This cucumber test is like this:
#smoke
Scenario: Update profile Smoke test.
Given I navigate to this test webpage
And In Test, I click the element of "test_link"
This 2 steps are located inside a gem, following this structure:
gemname/lib/features/step_definitions/web_shared_steps.rb
And (/^I navigate to this (.*?)$/) do |web|
web = '$' + web.downcase.gsub(' ', '_')
#browser.goto eval(web)
end
Then (/In (.*?), I click the element of "(.*?)"$/) do |page, element|
on_page(page + 'Page').click_element(element)
end
And the methods are also in the gem, following this structure:
gemname/lib/basic_methods.rb
module BasicMethods
include PageObject
include PageObject::PageFactory
def click_element (element)
element = element.downcase.gsub(' ', '_')
wait_until{send("#{element}?")}
select = send("#{element}_element")
wait_until{select.visible?}
select.click
end
So, If I execute with the command line "bundle exec cucumber", the test is able to find the step definitions in the gem, and the methods in the gem, and everything is executed correctly.
But, Rubymine is still giving an alert of "Undefined Step reference" for the two steps of the scenario, and I am unable to "command click" in those steps in order to navigate to the step definition.
QUESTION: Since the test in working, how is possible to "tell" Rubymine the folder/location where it has to search for the step definitions?
I don't have Rubymine so I can't verify this answer but it sounds very similar to a problem I had with Syntastic under Vim.
Try creating a file called features/support/external.rb containing:
require "lib/features/step_definitions/web_shared_steps.rb"
Actually it doesn't have to be called external.rb, it can be called anything except env.rb.
I'm guessing that Rubymine is calling cucumber with the --dry-run option to generate its warnings. See this answer for details.
try putting the gem source on your machine and then add this to your gemfile
gem 'gem', :path => 'path/to/gem-source'
I've had a similar problem with trying to use my own fork of a gem
When i did gem 'my-gem', :git => 'my-repo.git' i had the same problem as you, but by using :path I got rubyMine to recognise my step definitions.
I need to switch back to :git in production but at least i have a solution for my dev environment
I have some common methods used in a couple different specs, I want to extract them to some place like a spec helper that is accessible from all specs. Anyone know how to do this?
Here is something that sorta quacks like a spec_helper.
# _spec_helper.rb
module SpecHelper
::App::Persistence = {}
# global `before :each` ish
def self.extended(base)
base.before do
::App::Persistence.clear
end
end
def foo_helper
end
end
And then use it:
# my_view_spec.rb
describe "MyView" do
extend SpecHelper
before do
foo_helper
end
...
Two things to bear in mind:
Spec helper file is named in such way that it gets loaded first (leading underscore)
When running individual specs (e.g. files=my_view_spec.rb) helper file must go along - files=spec/my_view_spec.rb,spec/_spec_helper.rb
I just throw my common methods used in specs as they are (not encapsulated in a Module or anything) in a spec/support/utilities.rb file and Rubymotion seems to pick them up fine, though I don't know if this is the "proper" way to do this.
According to current http://www.rubymotion.com/developer-center/articles/testing/#_spec_helpers
Spec helpers are created under the spec/helpers directory of a RubyMotion project. An example could be spec/helpers/extension.rb.