I am trying following robot test case but it is not working.
my requirement is:
if 'bbb' is equal to 'aaa' than log to console "bbb is equal to aaa" and fail the test case. Don't run next conditions.
if 'bbb' is greater than 'aaa' than log to console "bbb is GREATER than aaa" and run next test if condition, if next if condition passes than print "bbb is greater than or equal to max and pass the test case.
how to do this? following script is running all if conditions. please help.
*** Settings ***
Documentation TEST aaa, bbb, max
Library SSHLibrary
Library String
Library DebugLibrary
*** Variables ***
${aaa} 5
${bbb} 10
${max} 15
*** Test Cases ***
Test aaa, bbb, max
[Documentation] Test aaa, bbb, max
Run Keyword If '${bbb}' == '${aaa}' log bbb is EQUAL to aaa Fail
Run Keyword If '${bbb}' > '${aaa}' log bbb is GREATER than aaa Pass
Run Keyword If '${bbb}' >= '${max}' log bbb is GREATER than max Pass
You define numeric variables but testing as strings.
Only the Log keyword would run, the Fail and Pass are ignored.
The test is currently passing without output because none of the conditions are True.
Here is a modified version of you test case:
*** Settings ***
Documentation TEST aaa, bbb, max
Library SSHLibrary
Library String
Library DebugLibrary
*** Variables ***
${aaa} 9
${bbb} 10
${max} 15
*** Test Cases ***
Test aaa, bbb, max
[Documentation] Test aaa, bbb, max
Run Keyword If ${bbb} == ${aaa} Fail bbb is EQUAL to aaa test will Fail
Run Keyword If ${bbb} > ${aaa} Log To Console bbb is GREATER than aaa test will Pass
Run Keyword If ${bbb} >= ${max} Log To Console bbb is GREATER OR EQUAL than max test will Pass
Run Keyword Unless ${bbb} > ${aaa} or ${bbb} >= ${max} Log To Console Condition not covered test will Pass
Related
In a package, I have a dozen test files which run in parallel with testthat edition 3. By default, they run with 2 CPUs, and I would like to run them with more.
The doc says:
Starting a new R process is relatively expensive, so testthat begins by creating a pool of workers. The size of the pool will be determined by getOption("Ncpus"), then the TESTTHAT_CPUS envvar. If neither are set, then two processes are started. In any case, testthat will never start more subprocesses than test files.
However, I don't understand where I should set the option so that my tests run on more than 2 CPUs (at least locally) when running tests from RStudio's "Build" pane.
I tried adding Sys.setenv(TESTTHAT_NCPUS =4) in my testing helper file, but that didn't have any effect.
A 2020 github issue hints about a potential line in DESCRIPTION (Config/testthat/ncpus) but it doesn't seem it has been implemented.
How could I increase the number of CPU used for testing?
If your individual tests are running in serial, then it could be one of: edition not 3; testthat/parallel not set; perhaps something else with your tests that disable parallel execution.
Reproducible demonstration of testthat edition 3 running in parallel.
devtools::create("somepkg") ; setwd("somepkg")
usethis::use_testthat(edition = 3)
Sys.getpid()
# [1] 16888
Edit DESCRIPTION, add the two Config/* lines, resulting in:
Package: somepkg
Title: What the Package Does (One Line, Title Case)
Version: 0.0.0.9000
Authors#R:
person("First", "Last", , "first.last#example.com", role = c("aut", "cre"),
comment = c(ORCID = "YOUR-ORCID-ID"))
Description: What the package does (one paragraph).
License: `use_mit_license()`, `use_gpl3_license()` or friends to pick a
license
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
Config/testthat/parallel: true
Config/testthat/edition: 3
I have no files in ./R/. I added this to tests/testthat.R:
library(testthat)
library(somepkg)
test_check("somepkg")
and these contents to four different files (test1.R through test4.R) within tests/testthat/:
test_that("this will fail", {
expect_equal(Sys.getpid(), 1)
})
(Yes, this is a stupid test, but since failures are clear in their failed expectations, this will report the actual PID in the summarized output.)
When I devtools::test() with no further changes, I see (among many lines removed):
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Failure (test2.R:2): this will fail
Sys.getpid() (`actual`) not equal to 1 (`expected`).
`actual`: 14584
`expected`: 1
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Failure (test1.R:2): this will fail
Sys.getpid() (`actual`) not equal to 1 (`expected`).
`actual`: 5792
`expected`: 1
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Failure (test3.R:2): this will fail
Sys.getpid() (`actual`) not equal to 1 (`expected`).
`actual`: 14584
`expected`: 1
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Failure (test4.R:2): this will fail
Sys.getpid() (`actual`) not equal to 1 (`expected`).
`actual`: 5792
`expected`: 1
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Since there are two PIDs listed (neither are 16888 from the parent process), this confirms that with no further action, 2 (separate) processes were used to run test files.
Sys.setenv(TESTTHAT_CPUS=4)
devtools::test()
# `actual`: 18692
# `actual`: 3844
# `actual`: 27160
# `actual`: 9716
Unset that to make sure we're fully reset,
Sys.setenv(TESTTHAT_CPUS="")
devtools::test()
# `actual`: 30372
# `actual`: 25928
# `actual`: 30372
# `actual`: 25928
Now using options:
options(Ncpus=4)
devtools::test()
# `actual`: 35012
# `actual`: 33888
# `actual`: 25208
# `actual`: 19292
I'm using robotframewok 4.0
I have defined a simple list variable:
*** Variables ***
${BROWSERSIZE} 1400 900
Using this variable in a test case:
Log To Console Size: ${BROWSERSIZE}, Width:${BROWSERSIZE}[0] Height:${BROWSERSIZE}[1]
the output is this:
Size: 1400 900, Width:1 Height:4
Shouldn't (according to the manual) the indexes [0] and [1] refer to the two values? Instead it seems they refer to the character...
What am I doing wrong?
You are declaring your variable as a scalar instead of as a list. When you indexing it, you are indexing a string,[0] is the first character 1, [1] is the second character 4.
Change $ to # to declare the variable as list.
*** Variables ***
#{BROWSERSIZE} 1400 900
Suppose if ${x} isdictionary and has the value {'a':'4','b':'3'}. Please see the below code. I am getting error after incrementing the value. And the type of this variable ${count} is 'int' before and after increment.
${count}= set variable 3
Dictionary Should Contain Value ${x} ${count} ##--------> True
${Count}= Evaluate ${Count}+1
Dictionary Should Contain Value ${x}} ${count} ##-------> Doesnot contain value '4'
your variable ${count} is at the beginning not an integer. It is string.
Integer has to be set like this in Robot Framework
${count}= set variable ${3}
After your Evaluate you have an integer because 3+1 evaluates to 4
You dictionary has strings as values. {'a':'4','b':'3'}
So the first call works, because you expect the string "3" in the dictionary.
The second call failed, because the integer 4 ins not equal to the string "4"
This code would work:
*** Settings ***
Library Collections
*** Variables ***
&{x} a=4 b=3
*** Test Cases ***
test
${count}= set variable 3
Dictionary Should Contain Value ${x} ${count}
${Count}= Evaluate str(${Count}+1)
Dictionary Should Contain Value ${x} ${count}
Consider the following usage of "Should Not Be Equal" keyword:
*** Test Cases ***
Use "Should Not Be Equal"
Should Not Be Equal 0b1011 11 # Should fail, but passes. Why?
Should Not Be Equal 0b1011 0xB # Should fail, but passes. Why?
The goal is to provide a negative failing test case by providing 0b1011 (i.e. 11 in base 10)
and 11 (in base 10). Since 11 == 11 is True (in base 10), this test case should fail.
The actual result is that the test case passes, why?
Because by default, all arguments to keywords are passed as strings. So this call:
Should Not Be Equal 0b1011 11
, is similar to python's
"0b1011" != "11"
, which evaluates to True.
If you want to check the integers/numerical values, this is the way:
Should Not Be Equal ${0b1011} ${11} # will fail, they are equal.
How to subtract the number in a Robot Framework?
What is the command for it?
For example, if I am getting a count, I want to subtract -1 and map keywords with the resulting value.
If your variable contains an actual number, you can use extended variable syntax. For example, this test will pass:
*** Variables ***
| ${count} | ${99} | # using ${} syntax coerces value to number
*** Test cases ***
| Example
| | Should be equal as numbers | ${count-1} | 98
You can also use the Evaluate keyword to create a python expression. For example:
*** Variables ***
| ${count} | 99
*** Test cases ***
| Example
| | ${count}= | Evaluate | ${count} - 1
| | Should be equal as numbers | ${count} | 98
Note: using Evaluate will work whether ${count} is a number or the string representation of a number.
You could use Evaluate keyword:
*** Test Cases ***
Stackoverflow
${x} = Set Variable 1
${y} = Evaluate ${x} - 1
An expression like this should work:
${token_expire_time} = Evaluate ${token_generate_time}-${expires_in}
If for some reason the conversion with ${} doesn't seem to work, then feel free to use:
Convert to integer keyword
or
Convert to number keyword