Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pytest option to randomly select and run number/percentage of tests from the collection. #15450

Merged

Conversation

jameerpathan111
Copy link
Contributor

@jameerpathan111 jameerpathan111 commented Jun 18, 2024

Problem Statement

  • The Automation team requested to add support for pytest option to randomly select and run number/percentage of tests from the collection.

Solution

  • Add pytest option --select-random-tests: Number/Percentage of tests to randomly select and run from selected test collection.
  • Add pytest option --random-seed: Seed value for random test collection. It should be used with --select-random-tests option.
  • To rerun the same test collection use the same seed value, check robottelo.log for seed value generated(if it was not provided before).

Examples

  • pytest tests/foreman/ --select-random-tests '5%'
  • pytest tests/foreman/ --select-random-tests 4 --random-seed fsdaxv

Related Issues

  • See SAT-20829

@jameerpathan111 jameerpathan111 added CherryPick PR needs CherryPick to previous branches 6.13.z Introduced in or relating directly to Satellite 6.13 6.14.z Introduced in or relating directly to Satellite 6.14 6.15.z Introduced in or relating directly to Satellite 6.15 labels Jun 18, 2024
@jameerpathan111 jameerpathan111 self-assigned this Jun 18, 2024
@jameerpathan111 jameerpathan111 requested a review from a team as a code owner June 18, 2024 14:32
@jameerpathan111
Copy link
Contributor Author

trigger: test-robottelo
pytest: tests/foreman/api/ --select-random-tests 2

@Satellite-QE
Copy link
Collaborator

PRT Result

Build Number: 7429
Build Status: SUCCESS
PRT Comment: pytest tests/foreman/api/ --select-random-tests 2 --external-logging
Test Result : ======== 2 passed, 2305 deselected, 2336 warnings in 661.87s (0:11:01) =========

@Satellite-QE Satellite-QE added the PRT-Passed Indicates that latest PRT run is passed for the PR label Jun 18, 2024
@jameerpathan111
Copy link
Contributor Author

trigger: test-robottelo
pytest: tests/foreman/api/test_location.py --select-random-tests '10%' --component OrganizationsandLocations

@Satellite-QE
Copy link
Collaborator

PRT Result

Build Number: 7431
Build Status: SUCCESS
PRT Comment: pytest tests/foreman/api/test_location.py --select-random-tests 10% --component OrganizationsandLocations --external-logging
Test Result : ========== 4 passed, 29 deselected, 65 warnings in 623.06s (0:10:23) ===========

Copy link
Member

@JacobCallahan JacobCallahan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you generate a random seed that would be logged before use? Also, adding the option to pass a specific seed would allow us to repeat a random test selection.

@jameerpathan111
Copy link
Contributor Author

Can you generate a random seed that would be logged before use? Also, adding the option to pass a specific seed would allow us to repeat a random test selection.

I don't completely understand what you mean here. As per @jyejare's request he'd like user to specify the number/percentage of tests to collect. Or did you mean if no value is provided we should pick random number of tests?

Copy link
Member

@ogajduse ogajduse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed with Jake. It should be really simple to implement. With the current implementation, it should be only a matter of running random.seed(seed).

def random_pick(items, k, seed=None):
    if seed is not None:
        random.seed(seed)
    return random.sample(items, k) 

items = [1,2,3,4,5,6,7,8,9,10]
k = 2
seed = 42

picked_items = random_pick(items, k, seed)
print(f"Picked {k} random items (seed: {seed}):", picked_items)
# Picked 2 random items (seed: 42): [2, 1]
picked_items = random_pick(items, k, seed)
print(f"Picked {k} random items (seed: {seed}):", picked_items)
#Picked 2 random items (seed: 42): [2, 1]
picked_items = random_pick(items, k, seed)
print(f"Picked {k} random items (seed: {seed}):", picked_items)
#Picked 2 random items (seed: 42): [2, 1]

Edit: If you will implement this logic, you will also need to define a new cmd option to allow the user to pass the seed.

Copy link
Contributor

@rmynar rmynar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree with @JacobCallahan, there should be a way to rerun the same set of tests. There's a good tip how to implement "seed" from @ogajduse. But if you're not going to implement the seed, there should be at least a log entry that contains all the selected test, which should be easily copy&pasted.

@Satellite-QE Satellite-QE removed the PRT-Passed Indicates that latest PRT run is passed for the PR label Jul 2, 2024
@jameerpathan111
Copy link
Contributor Author

  • pytest tests/foreman/ --select-random-tests 4

Ah, now I get what Jake meant. Thanks for explaining:) Implemented your suggestion.

@jameerpathan111
Copy link
Contributor Author

jameerpathan111 commented Jul 2, 2024

(env-robottelo3) jpathan@jpathan-mac robottelo % pytest tests/foreman/api/test_activationkey.py --select-random-tests 4 --collect-only --disable-warnings 
=============================================================================================================================================== test session starts ===============================================================================================================================================
collected 64 items / 60 deselected / 4 selected                                                                                                                                                                                                                                                                   

<Dir robottelo>
  <Package tests>
    <Package foreman>
      <Package api>
        <Module test_activationkey.py>
          <Function test_negative_update_limit[2]>
          <Function test_positive_update_name[alphanumeric]>
          <Function test_positive_add_host_collections>
          <Function test_positive_delete[numeric]>

================================================================================================================================== 4/64 tests collected (60 deselected) in 1.40s ==================================================================================================================================

Check the logs/robottelo.log for seed value.
2024-07-02 13:05:03 - robottelo - INFO - Modifying test collection based on --select-random-tests pytest option. Tests collected: 63, Tests to select randomly: 4, Seed value: FxACYYBRwf

(env-robottelo3) jpathan@jpathan-mac robottelo % pytest tests/foreman/api/test_activationkey.py --select-random-tests 4 --random-seed FxACYYBRwf --collect-only --disable-warnings
=============================================================================================================================================== test session starts ===============================================================================================================================================
collected 64 items / 60 deselected / 4 selected                                                                                                                                                                                                                                                                   

<Dir robottelo>
  <Package tests>
    <Package foreman>
      <Package api>
        <Module test_activationkey.py>
          <Function test_negative_update_limit[2]>
          <Function test_positive_update_name[alphanumeric]>
          <Function test_positive_add_host_collections>
          <Function test_positive_delete[numeric]>

================================================================================================================================== 4/64 tests collected (60 deselected) in 1.33s ==================================================================================================================================```

@jameerpathan111 jameerpathan111 requested a review from ogajduse July 2, 2024 10:27
@jameerpathan111 jameerpathan111 force-pushed the random_collection branch 5 times, most recently from aa86e2a to 0d7f239 Compare July 2, 2024 11:03
Copy link
Contributor

@rmynar rmynar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about Automation team needs, but current solution seems to be able to select testcases randomly and also replay the same set of tests. The replay using seed breaks as soon as test collection is modified, but I don't think this is a problem. ACK

Copy link
Member

@JacobCallahan JacobCallahan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice!!

@JacobCallahan JacobCallahan merged commit 600597e into SatelliteQE:master Jul 2, 2024
8 checks passed
github-actions bot pushed a commit that referenced this pull request Jul 2, 2024
…ts from the collection. (#15450)

Add pytest option to collect N random tests

(cherry picked from commit 600597e)
github-actions bot pushed a commit that referenced this pull request Jul 2, 2024
…ts from the collection. (#15450)

Add pytest option to collect N random tests

(cherry picked from commit 600597e)
github-actions bot pushed a commit that referenced this pull request Jul 2, 2024
…ts from the collection. (#15450)

Add pytest option to collect N random tests

(cherry picked from commit 600597e)
jyejare pushed a commit to jyejare/robottelo that referenced this pull request Oct 19, 2024
…ts from the collection. (SatelliteQE#15450)

Add pytest option to collect N random tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
6.13.z Introduced in or relating directly to Satellite 6.13 6.14.z Introduced in or relating directly to Satellite 6.14 6.15.z Introduced in or relating directly to Satellite 6.15 CherryPick PR needs CherryPick to previous branches
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants